////////////////////////////////////////////////////////////////////////////////


*This do-file produces the job security regression analyses and tables found in the main text.


////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////

*Table 2: regression of year dummy marginal effects (with and without controls) on unemployment rate and time trend

*Note that this section also produces Figures C.1-C.4, which plot the marginal effects of the year dummies (with controls), which are summarized in Table 2 in the main text.

////////////////////////////////////////////////////////////////////////////////

**PART ONE: UK

cd "$ukdata"

*With demographic controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##ib1.marital i.v*##i.uni i.v*##i.immigrant i.v*##i.nonwhite i.v*##ib1.isco i.v*##ib2001.year* if (insample==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d

**Construction of Average Marginal Effects (AMEs) requires manual procedure, since
*Poisson "trick" isn't a canned routine in Stata, and comparability of estimates
*across countries requires marginal effects of time dummies

poisson_year_mfx, save("temp/poisson_marginal_effects_controls.dta") controls

*Without controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##ib2001.year if (insample==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx, save("temp/poisson_marginal_effects_nocontrols.dta")

*Produce plot of marginal effects, controls and no controls
use "temp/poisson_marginal_effects_nocontrols.dta", clear

merge 1:1 year using "temp/poisson_marginal_effects_controls.dta"
	drop _merge
	drop if year==2018
	recode variance4_controls_0 .=0
	gen dydx_nocontrols=dprob1dx+dprob2dx
	gen dydx_controls=dprob1dx_controls+dprob2dx_controls
	gen se_controls= sqrt(variance1_controls+variance2_controls+variance1_controls_0+ variance2_controls_0)
	gen se_nocontrols= sqrt(variance1_nocontrols+variance2_nocontrols+variance1_2001+variance2_2001)
	gen ci_hi_controls=dydx_controls+1.96*se_controls
	gen ci_low_controls=dydx_controls-1.96*se_controls
	gen ci_hi_nocontrols=dydx_nocontrols+1.96*se_nocontrols
	gen ci_low_nocontrols=dydx_nocontrols-1.96*se_nocontrols
	gen year_controls=year+.2
	replace year=year-.2	
	
// Correlation of year AMEs with and without controls
corr dydx_controls dydx_nocontrols
di "Correlation==`r(rho)'"
	
*Figures comparing dummies with and without controls
cd "$ukgraphics"
	
graph twoway (scatter dydx_nocontrols year, msize(small) connect(direct) lcolor(gs0) mcolor(gs0)) (scatter dydx_controls year_controls, msize(small) connect(direct) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_nocontrols ci_low_nocontrols year, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_controls ci_low_controls year_controls, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) scale(.8) title("Implied marginal effects of year dummies on Pr(insecure)" "BHPS and US, 1991-2018") xtitle("Year") ytitle("Marginal effect") xlabel(1991/2017, angle(45)) yline(0, lcolor(black)) xline(2001, lpattern(dash) lcolor(gs0)) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI"))

graph export "year_fes_poisson.png", as(png) name("Graph") replace

*Regress AMEs on unemployment and time trend
cd "$ukdata"

drop year
replace year_controls=floor(year_controls)
rename year_controls year

merge 1:1 year using "clean/unrate.dta"
keep if _merge==3
drop _merge
replace urate=urate*100
rename urate unrate
gen t=year-1991

*Variable rescaling
replace t = t/10
replace dydx_nocontrols=dydx_nocontrols*100
replace dydx_controls=dydx_controls*100

reg dydx_nocontrols unrate t, vce(robust)
estimates save bhps_nocontrols, replace
reg dydx_controls unrate t, vce(robust)
estimates save bhps_controls, replace

//save base year mean for table
use "clean/uk_insecure_summary.dta", clear
sum(insecure) if year==2001
scalar def m = r(mean)*100

estimates use bhps_nocontrols
estadd scalar baseline_mean = m
estimates store bhps_nocontrols
estimates use bhps_controls
estadd scalar baseline_mean = m
estimates store bhps_controls

**PART TWO, v1: US (GSS)

cd "$usdata"

use "clean/gss_clean.dta", clear

*Year dummies without controls	
logit insecure ib2002.year if (insample==1) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/nocontrols.dta, replace)
	
*Year dummies with controls
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.union i.immigrant i.uni i.nonwhite ib5.marital if (insample==1) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/controls.dta, replace)

use "temp/nocontrols.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols se_nocontrols)
tempfile nocontrols
save `nocontrols'

use "temp/controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `nocontrols'	
	rename (_margin _ci_lb _ci_ub _se_margin) (dydx_controls ci_lower_controls ci_upper_controls se_controls)
	keep year dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols dydx_controls ci_upper_controls ci_lower_controls se_controls se_nocontrols
	gen year_controls=year+.2
	replace year=year-.2
	set obs 22
	replace dydx_nocontrols=0 in 22
	replace dydx_controls=0 in 22
	replace ci_upper_controls=0 in 22
	replace ci_lower_controls=0 in 22
	replace ci_upper_nocontrols=0 in 22
	replace ci_lower_nocontrols=0 in 22
	replace year=2001.8 in 22
	replace year_controls=2002.2 in 22
	sort year

// Correlation of year AMEs with and without controls
corr dydx_controls dydx_nocontrols
di "Correlation==`r(rho)'"
	
*Figure comparing dummies with and without controls	
cd "$usgraphics"

twoway (scatter dydx_nocontrols year, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_controls year_controls, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_nocontrols ci_lower_nocontrols year, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_controls ci_lower_controls year_controls, msize(0) lcolor(gs10) mcolor(gs10)), xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI")) graphregion(color(white)) bgcolor(white) title("Marginal effects of year dummies on Pr(Insecure)" "US 1977-2018") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "gss_year_dummies.png", as(png) name("Graph") replace

*Regress AMEs on unemployment and time trend
cd "$usdata"

drop year
replace year_controls=floor(year_controls)
rename year_controls year
drop if year==.

merge 1:1 year using "clean/unrate.dta"
keep if _merge==3
drop _merge
replace unrate=unrate*100
gen t=year-1978

*Variable rescaling
replace t = t/10
replace dydx_nocontrols=dydx_nocontrols*100
replace dydx_controls=dydx_controls*100

reg dydx_nocontrols unrate t, vce(robust)
estimates save gss_nocontrols, replace
reg dydx_controls unrate t, vce(robust)
estimates save gss_controls, replace

//save base year mean for table
use "clean/us_insecure_summary.dta", clear
sum(insecure) if year==2002
scalar def m = r(mean)*100

estimates use gss_nocontrols
estadd scalar baseline_mean = m
estimates store gss_nocontrols
estimates use gss_controls
estadd scalar baseline_mean = m
estimates store gss_controls

**PART TWO, v2: US (HRS)

cd "$usdata"

use "clean/hrs_clean.dta", clear

*Year dummies without controls	
reg insecurity ib2002.year if (insample==1) [pw=wtresp], vce(cluster year)
	margins, dydx(i.year) saving(temp/nocontrols_hrs.dta, replace)
	
*Year dummies with controls
reg insecurity ib2002.year i.parttime age age_squared i.male i.union i.immigrant i.uni i.nonwhite ib5.marital i.industry if (insample==1) [pw=wtresp], vce(cluster year)
	margins, dydx(i.year) saving(temp/controls_hrs.dta, replace)

use "temp/nocontrols_hrs.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols se_nocontrols)
tempfile nocontrols
save `nocontrols'

use "temp/controls_hrs.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `nocontrols'
	rename (_margin _ci_lb _ci_ub) (dydx_controls ci_lower_controls ci_upper_controls)
	keep year dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols dydx_controls ci_upper_controls ci_lower_controls
	gen year_controls=year+.2
	replace year=year-.2
	set obs 12
	replace dydx_nocontrols=0 in 12
	replace dydx_controls=0 in 12
	replace ci_upper_controls=0 in 12
	replace ci_lower_controls=0 in 12
	replace ci_upper_nocontrols=0 in 12
	replace ci_lower_nocontrols=0 in 12
	replace year=2001.8 in 12
	replace year_controls=2002.2 in 12
	sort year

// Correlation of year AMEs with and without controls
corr dydx_controls dydx_nocontrols
di "Correlation==`r(rho)'"
	
*Figure comparing dummies with and without controls	
cd "$usgraphics"

twoway (scatter dydx_nocontrols year, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_controls year_controls, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_nocontrols ci_lower_nocontrols year, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_controls ci_lower_controls year_controls, msize(0) lcolor(gs10) mcolor(gs10)), xlabel(1994(2)2016, angle(45)) scale(.8) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI")) graphregion(color(white)) bgcolor(white) title("Marginal effects of year dummies on Pr(Insecure)" "US 1994-2016") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "hrs_year_dummies.png", as(png) name("Graph") replace

*Regress AMEs on unemployment and time trend
cd "$usdata"

drop year
replace year_controls=floor(year_controls)
rename year_controls year
drop if year==.

merge 1:1 year using "clean/unrate.dta"
keep if _merge==3
drop _merge
replace unrate=unrate*100
gen t=year-1994

*Variable rescaling
replace t = t/10
replace dydx_nocontrols=dydx_nocontrols*100
replace dydx_controls=dydx_controls*100

reg dydx_nocontrols unrate t, vce(robust) 
estimates save hrs_nocontrols, replace
reg dydx_controls unrate t, vce(robust)
estimates save hrs_controls, replace

//save base year mean for table
use "clean/us_insecure_summary.dta", clear
sum(insecurity_hrs) if year==2002
scalar def m = r(mean)*100

estimates use hrs_nocontrols
estadd scalar baseline_mean = m
estimates store hrs_nocontrols
estimates use hrs_controls
estadd scalar baseline_mean = m
estimates store hrs_controls

**PART THREE: Germany

cd "$germanydata"

use "clean/soep_clean.dta", clear

*Year dummies without controls
logit insecure ib2001.year if insample==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_nocontrols.dta, replace)

*Year dummies with controls
logit insecure ib2001.year i.temp i.parttime i.semp i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.marginal i.uni i.ddr if insample==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_controls.dta, replace)
	
use "temp/soep_nocontrols.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx ci_upper ci_lower se)
tempfile nocontrols
save `nocontrols'

use "temp/soep_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `nocontrols'
	rename (_margin _ci_lb _ci_ub _se) (dydx_controls ci_lower_controls ci_upper_controls se_controls)
	keep year dydx ci_upper ci_lower dydx_controls ci_upper_controls ci_lower_controls se_controls se
	gen year_controls=year+.15
	replace year=year-.15
	set obs 34
	replace dydx=0 in 34
	replace dydx_controls=0 in 34
	replace ci_upper_controls=0 in 34
	replace ci_lower_controls=0 in 34
	replace ci_upper=0 in 34
	replace ci_lower=0 in 34
	replace year=2000.85 in 34
	replace year_controls=2001.15 in 34
	sort year
	
// Correlation of year AMEs with and without controls
corr dydx_controls dydx
di "Correlation==`r(rho)'"	
	
*Figure comparing dummies with and without controls	
cd "$germanygraphics"

twoway (scatter dydx year, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_controls year_controls, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper ci_lower year, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_controls ci_lower_controls year_controls, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)") yline(0, lcolor(gs0)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "soep_year_dummies.png", as(png) name("Graph") replace

*Regress AMEs on unemployment rate and linear time trend
cd "$germanydata"

drop year
replace year_controls=floor(year_controls)
rename year_controls year

merge 1:1 year using "clean/unemployment.dta"
keep if _merge==3
drop _merge
rename (urate dydx se) (unrate dydx_nocontrols se_nocontrols)
replace unrate=unrate*100
gen t=year-1985
drop if year==1984

*Variable rescaling
replace t = t/10
replace dydx_nocontrols=dydx_nocontrols*100
replace dydx_controls=dydx_controls*100

reg dydx_nocontrols unrate t, vce(robust)
estimates save soep_nocontrols, replace
reg dydx_controls unrate t, vce(robust)
estimates save soep_controls, replace

//save base year mean for table
use "clean/soep_insecure_summary.dta", clear
sum(insecure) if year==2001
scalar def m = r(mean)*100

estimates use soep_nocontrols
estadd scalar baseline_mean = m
estimates store soep_nocontrols
estimates use soep_controls
estadd scalar baseline_mean = m
estimates store soep_controls

**TABLE 2**
esttab gss_nocontrols gss_controls hrs_nocontrols hrs_controls bhps_nocontrols bhps_controls soep_nocontrols soep_controls using "$tables/unrate_t_regs.tex", replace nonumber r2 cells(b(fmt(%9.3g) star) se(fmt(%9.3g) par(( )))) keep(unrate t) coeflabels(t "Linear time trend*10" unrate "Unemployment rate*100") mlabels("No controls" "Controls" "No controls" "Controls" "No controls" "Controls" "No controls" "Controls") booktab collabels("") mgroups("US (GSS)" "US (HRS)" "UK" "Germany", pattern(1 0 1 0 1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(})) postfoot("\tabnotes{9}{This table displays the results of regressions of the average marginal effects of the year dummies (with and without controls) on the unemployment rate and a linear time trend using the GSS, HRS, BHPS/UKHLS, and SOEP data. Standard errors in parentheses. *p < 0.05, **p < 0.01 ***p < 0.001}") title("Linear time trend and unemployment rate regression results") scalar("baseline_mean Baseline year insecurity") sfmt(%9.1f)


////////////////////////////////////////////////////////////////////////////////

*Table 3: Marginal effects of covariates of interest in full specification

////////////////////////////////////////////////////////////////////////////////

**PART ONE: UK

cd "$ukdata"

**Run main specification with controls so we can get coefficients and vcov matrix 
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##ib1.marital i.v*##i.uni i.v*##i.immigrant i.v*##i.nonwhite i.v*##ib1.isco i.v*##ib2001.year* if (insample==1), a(person_year_id) vce(cluster year) d
	matrix define b=e(b)
	matrix define V=e(V)

*Generate predicted probabilities and SEs of BHPS responses
	predict linear_predict, xb
	predict se, stdp
	drop if linear_predict==.
save "temp/poisson_with_predictions_controls.dta", replace

*Compute average marginal effects of variables of interest
poisson_covariates_mfx temp parttime semp semp_boss union male uni immigrant nonwhite

use "temp/poisson_with_predictions_controls", clear
poisson_covariates_mfx jobtenure age age_squared, continuous

*Merge all AMEs together
use "ames/temp_new.dta", clear
	foreach var in semp semp_boss immigrant nonwhite parttime uni union male age age_squared jobtenure {
		append using "ames/`var'_new.dta"
	}
save "ames/all.dta", replace
	
*Put UK AMEs in an estimates form so they can be put in a table with US and
*Germany AMEs
cd "$ukdata"

use "clean/uk_poisson_reverse.dta", clear
	cou if insample
	local n = round(r(N)/4)

use "ames/all.dta", clear
	mkest, replace v(se) b(ame) obs(`n')
	estimates save bhps, replace
	
	//save base year mean for table
	use "clean/uk_linear_reverse.dta", clear
	gen insecure = .
	replace insecure = ukhls1+ukhls2 if inrange(wave,1,18)
	replace insecure = 1 if wave>=19&inrange(ukhls_security,1,2)
	replace insecure=0 if wave>=19&inrange(ukhls_security,3,4)
	
	svyset _n [pw=relative_xw]
	
	svy: mean insecure if insample
	mat def help = e(b)
	local m = help[1,1]
	
	estimates use bhps
	estadd scalar baseline_mean = `m'
	estimates store bhps

**PART TWO: US
*US v1: GSS
cd "$usdata"

use "clean/gss_clean.dta", clear	
	rename (semp immigrant nonwhite parttime uni union male age age_squared) (r2 r4 r5 r6 r7 r8 r9 r10 r11)

logit insecure ib2002.year r2 r6 i.onedigit_naics r10 r11 r9 r8 r4 r7 r5 ib5.marital if insample [pw=wtssall], vce(cluster year)
	margins, dydx(r2 r4 r5 r6 r7 r8 r9 r10 r11) at((asobserved) _all year=2002) post
	estimates save gss, replace
	matrix US = e(b)
	
logit insecure ib2002.year r2 r6 i.onedigit_naics r10 r11 r9 r8 r4 r7 r5 ib5.marital if insample [pw=wtssall], vce(cluster year)
	margins, at((asobserved) _all year=2002) post
	mat def help = e(b)
	local m = help[1,1]
	
//save base year mean for table
estimates use gss
estadd scalar baseline_mean = `m'
estimates store gss

*US v2: HRS
cd "$usdata"	
use "clean/hrs_clean.dta", clear

rename (semp immigrant nonwhite parttime uni union male age age_squared) (r2 r4 r5 r6 r7 r8 r9 r10 r11)

reg insecurity ib2002.year r6 r10 r11 r4 r5 r7 r8 r9 ib5.marital if (insample==1) [pw=wtresp], vce(cluster year)
	margins, dydx(r4 r5 r6 r7 r8 r9 r10 r11) at((asobserved) _all year=2002) post
	estimates save hrs, replace

reg insecurity ib2002.year r6 r10 r11 r4 r5 r7 r8 r9 ib5.marital if (insample==1) [pw=wtresp], vce(cluster year)	
	margins, at((asobserved) _all year=2002) post
	mat def help = e(b)
	local m = help[1,1]
	
	//save base year mean for table
	estimates use hrs
	estadd scalar baseline_mean = `m'
	estimates store hrs

**PART THREE: Germany

cd "$germanydata"

use "clean/soep_clean.dta", clear
rename (temp semp immigrant parttime uni male age age_squared jobtenure marginal) (r1 r2 r4 r6 r7 r9 r10 r11 r12 r13)

logit insecure ib2001.year r1 r6 r2 r4 ib3.marital ib1.isco r9 r10 r11 r7 r12 r13 if insample [pw=xw], vce(cluster year)
	margins, dydx(r1 r2 r4 r6 r7 r9 r10 r11 r12 r13) at((asobserved) _all year=2001) post
	estimates save soep, replace
	matrix DE = e(b)
	
logit insecure ib2001.year r1 r6 r2 r4 ib3.marital ib1.isco r9 r10 r11 r7 r12 r13 if insample [pw=xw], vce(cluster year)	
	margins, at((asobserved) _all year=2001) post
	mat def help = e(b)
	local m = help[1,1]
	
	//save base year mean for table
	estimates use soep
	estadd scalar baseline_mean = `m'
	estimates store soep

**TABLE 3**

cd "$tables"

esttab gss hrs bhps soep using "covariate_ames.tex", order(r1 r2 r3 r4 r5 r6 r13 r7 r8 r9 r10 r11 r12) se title("Average marginal effects of covariates on probability of insecurity") coeflabels(r2 "Self-employed without employees$^a$" r3 "Self-employed with employees" r10 "Age" r11 "(Age/10)$^2$" r8 "Union active at workplace" r9 "Male" r7 "Higher education degree" r1 "Temporary employee" r5 "Non-white" r4 "Immigrant" r6 "Part-time worker" r12 "Length of job tenure" r13 "Marginally employed") mtitles("US (GSS)" "US (HRS)" "UK" "Germany") nonumbers gaps booktab replace b(%6.0g) se(%6.0g) postfoot("\tabnotes{5}{This table displays the marginal effects of covariates of interest on the probability of feeling insecure, derived from the" "logistic regressions specified in Appendix B for the US and Germany, and from the Poisson regression, specified" "in Appendix C, used to analyze the UK data." "a: This row gives the marginal effect for all self-employed (with and without employees) for the US and Germany, as" "the GSS and SOEP lack data on the breakdown of the self-employed by number of employees. Standard error in parentheses. *\textit{p} < 0.05, **\textit{p} < 0.01, ***\textit{p} < 0.001}") scalar(N "baseline_mean Baseline insecurity") width(\hsize)

////////////////////////////////////////////////////////////////////////////////

*Tables 4 and 5: regressing marginal effects of year dummies (with controls) on unemployment and time trend, by subgroup

*Note that this section of the code also produces figures F.1-F.7, which plot the marginal effects of these year dummies by subgroup.

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
*Step 1: run regressions on each subgroup and save year dummy marginal effects to be combined for table later
////////////////////////////////////////////////////////////////////////////////

**PART ONE: UK

cd "$ukdata"

use "clean/uk_poisson_reverse.dta", clear

*Dummy regression to save empty estimates
ppmlhdfe choice i.v*##ib2001.year if (insample==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d
estimates save bhps, replace

**New starters vs. incumbent workers
*New starters, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&jobtenure==0) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if jobtenure==0, save("temp/new_controls.dta") controls

*Non-new starters, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&jobtenure>0) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if jobtenure>0, save("temp/old_controls.dta") controls
	
use "temp/old_controls.dta", clear
	rename (dprob* variance*) (dprob*_old variance*_old)
merge 1:1 year using "temp/new_controls.dta"
	gen dydx_old=dprob1dx_controls_old+dprob2dx_controls_old
	gen dydx_new=dprob1dx_controls+dprob2dx_controls
	gen se_old=sqrt(variance1_controls_old+variance1_controls_0_old+variance2_controls_old+variance2_controls_0_old)
	replace se_old=0 if year==2001
	gen se_new=sqrt(variance1_controls+variance1_controls_0+variance2_controls+variance2_controls_0)
	replace se_new=0 if year==2001
	gen ci_hi_new=dydx_new+1.96*se_new
	gen ci_low_new=dydx_new-1.96*se_new
	gen ci_hi_old=dydx_old+1.96*se_old
	gen ci_low_old=dydx_old-1.96*se_old
	gen year_new=year+.2
	replace year=year-.2
	rename year year_old
save "temp/bhps_jobtenure.dta", replace

twoway (scatter dydx_new year_new, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_old year_old, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_new ci_low_new year_new, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_old ci_low_old year_old, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "New starters") label(2 "Non-new starters") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$ukgraphics/bhps_jobtenure_controls.png", as(png) name("Graph") replace

// cap erase "temp/new_controls.dta"
// cap erase "temp/old_controls.dta"

**Over-50s vs. prime age (25-49) vs. under-25s
*Over 50, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&age>=50) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if age>=50, save("temp/over50_controls.dta") controls

*Prime age (25-49), controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&inrange(age,25,49)) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if inrange(age,25,49), save("temp/primeage_controls.dta") controls

*Under 25, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&age<25) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if age<25, save("temp/under25_controls.dta") controls
	
use "temp/over50_controls.dta", clear
	rename (dprob* variance*) (dprob*_over variance*_over)
merge 1:1 year using "temp/primeage_controls.dta"
	gen dydx_over=dprob1dx_controls_over+dprob2dx_controls_over
	gen se_over=sqrt(variance1_controls_over+variance2_controls_over+variance1_controls_0_over+variance2_controls_0_over)
	replace se_over=0 if year==2001
	gen ci_hi_over=dydx_over+1.96*se_over
	gen ci_low_over=dydx_over-1.96*se_over
	gen dydx_primeage=dprob1dx_controls+dprob2dx_controls
	gen se_primeage=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	replace se_primeage=0 if year==2001
	gen ci_hi_primeage=dydx_primeage+1.96*se_primeage
	gen ci_low_primeage=dydx_primeage-1.96*se_primeage
	gen year_primeage=year+.2
	gen year_over=year-.2
	keep dydx* se* ci* year*
merge 1:1 year using "temp/under25_controls.dta"
	gen dydx_under=dprob1dx_controls+dprob2dx_controls
	gen se_under=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	replace se_under=0 if year==2001
	gen ci_hi_under=dydx_under+1.96*se_under
	gen ci_low_under=dydx_under-1.96*se_under
	rename year year_under
	
save "temp/bhps_age.dta", replace

twoway (scatter dydx_over year_over, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_primeage year_primeage, connect(direct) msize(small) lcolor(gs7) mcolor(gs7)) (scatter dydx_under year_under, connect(direct) msize(small) lcolor(gs12) mcolor(gs12)) (rcap ci_hi_over ci_low_over year_over, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_primeage ci_low_primeage year_primeage, msize(0) lcolor(gs7) mcolor(gs7)) (rcap ci_hi_under ci_low_under year_under, msize(0) lcolor(gs12) mcolor(gs12)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Over 50") label(2 "25-49") label(3 "Under 25") label(4 "95% CI") label(5 "95% CI") label(6 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$ukgraphics/bhps_age_controls.png", as(png) name("Graph") replace

// cap erase "temp/over50_controls.dta"
// cap erase "temp/primeage_controls.dta"
// cap erase "temp/under25_controls.dta"

**Men vs. women
*Men, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&male==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if male==1, save("temp/male_controls.dta") controls

*Women, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&male==0) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if male==0, save("temp/female_controls.dta") controls

use "temp/male_controls.dta", clear
	rename (dprob* variance*) (dprob*_male variance*_male)
merge 1:1 year using "temp/female_controls.dta"
	gen dydx_male=dprob1dx_controls_male+dprob2dx_controls_male
	gen se_male=sqrt(variance1_controls_male+variance2_controls_male+variance1_controls_0_male+variance2_controls_0_male)
	replace se_male=0 if year==2001
	gen ci_hi_male=dydx_male+1.96*se_male
	gen ci_low_male=dydx_male-1.96*se_male
	gen dydx_female=dprob1dx_controls+dprob2dx_controls
	gen se_female=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	replace se_female=0 if year==2001
	gen ci_hi_female=dydx_female+1.96*se_female
	gen ci_low_female=dydx_female-1.96*se_female
	gen year_female=year+.2
	replace year=year-.2
	rename year year_male

save "temp/bhps_sex.dta", replace	
	
twoway (scatter dydx_male year_male, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_female year_female, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_male ci_low_male year_male, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_female ci_low_female year_female, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Male") label(2 "Female") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$ukgraphics/bhps_sex_controls.png", as(png) name("Graph") replace

// cap erase "temp/male_controls.dta"
// cap erase "temp/female_controls.dta"

**Educated vs. uneducated
*Educated, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&uni==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if uni==1, save("temp/educated_controls.dta") controls

*Uneducated, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if insample==1&uni==0, a(person_year_id) vce(cluster year) d

poisson_year_mfx if uni==0, save("temp/uneducated_controls.dta") controls

use "temp/educated_controls.dta", clear
	rename (dprob* variance*) (dprob*_educated variance*_educated)
merge 1:1 year using "temp/uneducated_controls.dta"
	gen dydx_educated=dprob1dx_controls_educated+dprob2dx_controls_educated
	gen se_educated=sqrt(variance1_controls_educated+variance2_controls_educated+variance1_controls_0_educated+variance2_controls_0_educated)
	replace se_educated=0 if year==2001
	gen ci_hi_educated=dydx_educated+1.96*se_educated
	gen ci_low_educated=dydx_educated-1.96*se_educated
	gen dydx_uneducated=dprob1dx_controls+dprob2dx_controls
	gen se_uneducated=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	replace se_uneducated=0 if year==2001
	gen ci_hi_uneducated=dydx_uneducated+1.96*se_uneducated
	gen ci_low_uneducated=dydx_uneducated-1.96*se_uneducated
	gen year_uneducated=year+.2
	replace year=year-.2
	rename year year_educated
	
save "temp/bhps_educ.dta", replace

twoway (scatter dydx_educated year_educated, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_uneducated year_uneducated, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_educated ci_low_educated year_educated, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_uneducated ci_low_uneducated year_uneducated, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "University or professional degree") label(2 "No university degree") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$ukgraphics/bhps_education_controls.png", as(png) name("Graph") replace

// cap erase "temp/uneducated_controls.dta"
// cap erase "temp/educated_controls.dta"

**Temporary vs. permanent workers
*Temp, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&temp==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d
	
poisson_year_mfx if temp==1, save("temp/temp_controls.dta") controls

*Perm, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&temp==0) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if temp==0, save("temp/perm_controls.dta") controls

use "temp/temp_controls.dta", clear
	rename (dprob* variance*) (dprob*_temp variance*_temp)
merge 1:1 year using "temp/perm_controls.dta"
	gen dydx_temp=dprob1dx_controls_temp+dprob2dx_controls_temp
	gen se_temp=sqrt(variance1_controls_temp+variance2_controls_temp+variance1_controls_0_temp+variance2_controls_0_temp)
	replace se_temp=0 if year==2001
	gen ci_hi_temp=dydx_temp+1.96*se_temp
	gen ci_low_temp=dydx_temp-1.96*se_temp
	gen dydx_perm=dprob1dx_controls+dprob2dx_controls
	gen se_perm=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	replace se_perm=0 if year==2001
	gen ci_hi_perm=dydx_perm+1.96*se_perm
	gen ci_low_perm=dydx_perm-1.96*se_perm
	gen year_perm=year+.2
	replace year=year-.2
	rename year year_temp

save "temp/bhps_temp.dta", replace

twoway (scatter dydx_temp year_temp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_perm year_perm, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_temp ci_low_temp year_temp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_perm ci_low_perm year_perm, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Temporary workers") label(2 "Permanent workers") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$ukgraphics/bhps_temp_controls.png", as(png) name("Graph") replace

// cap erase "temp/bhps_perm_controls.dta"
// cap erase "temp/bhps_temp_controls.dta"

*Self-employed vs not self-employed
*Semp, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&semp==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if semp==1, save("temp/semp_controls.dta") controls

*Nonsemp, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&semp==0) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if semp==0, save("temp/nonsemp_controls.dta") controls

use "temp/semp_controls.dta", clear
	rename (dprob* variance*) (dprob*_semp variance*_semp)
merge 1:1 year using "temp/nonsemp_controls.dta"
	gen dydx_semp=dprob1dx_controls_semp+dprob2dx_controls_semp
	gen se_semp=sqrt(variance1_controls_semp+variance2_controls_semp+variance1_controls_0_semp+variance2_controls_0_semp)
	replace se_semp=0 if year==2001
	gen ci_hi_semp=dydx_semp+1.96*se_semp
	gen ci_low_semp=dydx_semp-1.96*se_semp
	gen dydx_nonsemp=dprob1dx_controls+dprob2dx_controls
	gen se_nonsemp=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	replace se_nonsemp=0 if year==2001
	gen ci_hi_nonsemp=dydx_nonsemp+1.96*se_nonsemp
	gen ci_low_nonsemp=dydx_nonsemp-1.96*se_nonsemp
	gen year_nonsemp=year+.2
	replace year=year-.2
	rename year year_semp

save "temp/bhps_semp.dta", replace

twoway (scatter dydx_semp year_semp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_nonsemp year_nonsemp, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_semp ci_low_semp year_semp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_nonsemp ci_low_nonsemp year_nonsemp, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Self-employed") label(2 "Not self-employed") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$ukgraphics/bhps_semp_controls.png", as(png) name("Graph") replace

// cap erase "temp/bhps_semp_controls.dta"
// cap erase "temp/bhps_nonsemp_controls.dta"

**Part-time workers vs. full-time workers
*Part time, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&parttime==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if parttime==1, save("temp/parttime_controls.dta") controls

*Full time, controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##i.nonwhite i.v*##i.immigrant i.v*##ib1.marital i.v*##i.uni i.v*##ib1.isco i.v*##ib2001.year* if (insample==1&parttime==0) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx if parttime==0, save("temp/fulltime_controls.dta") controls

use "temp/parttime_controls.dta", clear
	rename (dprob* variance*) (dprob*_parttime variance*_parttime)
merge 1:1 year using "temp/fulltime_controls.dta"
	gen dydx_parttime=dprob1dx_controls_parttime+dprob2dx_controls_parttime
	gen se_parttime=sqrt(variance1_controls_parttime+variance2_controls_parttime+variance1_controls_0_parttime+variance2_controls_0_parttime)
	replace se_parttime=0 if year==2001
	gen ci_hi_parttime=dydx_parttime+1.96*se_parttime
	gen ci_low_parttime=dydx_parttime-1.96*se_parttime
	gen dydx_fulltime=dprob1dx_controls+dprob2dx_controls
	gen se_fulltime=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	replace se_fulltime=0 if year==2001
	gen ci_hi_fulltime=dydx_fulltime+1.96*se_fulltime
	gen ci_low_fulltime=dydx_fulltime-1.96*se_fulltime
	gen year_fulltime=year+.2
	replace year=year-.2
	rename year year_parttime
	
save "temp/bhps_parttime.dta", replace

twoway (scatter dydx_parttime year_parttime, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_fulltime year_fulltime, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_parttime ci_low_parttime year_parttime, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_fulltime ci_low_fulltime year_fulltime, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Part-time") label(2 "Full-time") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$ukgraphics/bhps_parttime_controls.png", as(png) name("Graph") replace

// cap erase "temp/fulltime_controls.dta"
// cap erase "temp/parttime_controls.dta"

////////////////////////////////////////////////////////////////////////////////
*PART TWO, v1: US (GSS)
////////////////////////////////////////////////////////////////////////////////

cd "$usdata"

*Dummy regression to save empty estimates
use "clean/gss_clean.dta", clear
logit insecure ib2002.year if insample [pw=wtssall], vce(cluster year)	
estimates save gss, replace

*Men vs. women
use "clean/gss_clean.dta", clear
	
*With controls
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&male==1) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_male_controls.dta, replace)
	
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&male==0) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_female_controls.dta, replace)	

*Produce plot with controls	
use "temp/gss_male_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_male ci_upper_male ci_lower_male)
tempfile male
save `male'

use "temp/gss_female_controls.dta"
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `male'
	rename (_margin _ci_lb _ci_ub) (dydx_female ci_lower_female ci_upper_female)
	keep year dydx* ci_lower* ci_upper*
	gen year_male=year+.3
	replace year=year-.3
	rename year year_female
	set obs 22
	replace dydx_male=0 in 22
	replace dydx_female=0 in 22
	replace ci_upper_male=0 in 22
	replace ci_lower_male=0 in 22
	replace ci_upper_female=0 in 22
	replace ci_lower_female=0 in 22
	replace year_female=2001.7 in 22
	replace year_male=2002.3 in 22
	sort year_male
	
save "temp/gss_sex.dta", replace
	
twoway (scatter dydx_male year_male, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_female year_female, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_male ci_lower_male year_male, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_female ci_lower_female year_female, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Male") label(2 "Female") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "US 1977-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$usgraphics/gss_sex_controls.png", as(png) name("Graph") replace

// cap erase "temp/gss_male_controls.dta"
// cap erase "temp/gss_female_controls.dta"
// cap erase "temp/gss_male_nocontrols.dta"
// cap erase "temp/gss_female_nocontrols.dta"

*Over-50s vs. prime age (25-49) vs. under-25s
use "clean/gss_clean.dta", clear
	
*With controls
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&age>=50) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_over50_controls.dta, replace)
	
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&inrange(age,25,49)) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_primeage_controls.dta, replace)	
	
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&age<25) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_under25_controls.dta, replace)	

*Produce plot with controls	
use "temp/gss_over50_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_over ci_upper_over ci_lower_over se_over)
tempfile over
save `over'

use "temp/gss_primeage_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_primeage ci_upper_primeage ci_lower_primeage se_primeage)
tempfile prime
save `prime'

use "temp/gss_under25_controls.dta"
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `over', nogen
merge 1:1 year using `prime', nogen
	
	rename (_margin _ci_lb _ci_ub) (dydx_under ci_lower_under ci_upper_under)
	keep year dydx* ci_lower* ci_upper*
	gen year_over=year+.3
	gen year_under=year-.3
	rename year year_primeage
	set obs 22
	replace dydx_over=0 in 22
	replace dydx_under=0 in 22
	replace dydx_primeage=0 in 22
	replace ci_upper_over=0 in 22
	replace ci_lower_over=0 in 22
	replace ci_upper_primeage=0 in 22
	replace ci_lower_primeage=0 in 22
	replace ci_upper_under=0 in 22
	replace ci_lower_under=0 in 22
	replace year_under=2001.7 in 22
	replace year_primeage=2002 in 22
	replace year_over=2002.3 in 22
	sort year_over
	
save "temp/gss_age.dta", replace
	
twoway (scatter dydx_over year_over, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_primeage year_primeage, connect(direct) msize(small) lcolor(gs7) mcolor(gs7)) (scatter dydx_under year_under, connect(direct) msize(small) lcolor(gs12) mcolor(gs12)) (rcap ci_upper_over ci_lower_over year_over, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_primeage ci_lower_primeage year_primeage, msize(0) lcolor(gs7) mcolor(gs7)) (rcap ci_upper_under ci_lower_under year_under, msize(0) lcolor(gs12) mcolor(gs12)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Over 50") label(2 "25-49") label(3 "Under 25") label(4 "95% CI") label(5 "95% CI") label(6 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "US 1977-2018") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$usgraphics/gss_age_controls.png", as(png) name("Graph") replace

// cap erase "temp/gss_over50_nocontrols.dta"
// cap erase "temp/gss_primeage_nocontrols.dta"
// cap erase "temp/gss_under25_nocontrols.dta"
// cap erase "temp/gss_over50_controls.dta"
// cap erase "temp/gss_primeage_controls.dta"
// cap erase "temp/gss_under25_controls.dta"

**Part-time vs. full-time
use "clean/gss_clean.dta", clear
	
*With controls
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&parttime==1) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_parttime_controls.dta, replace)
	
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&parttime==0) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_fulltime_controls.dta, replace)	

*Produce plot with controls	
use "temp/gss_parttime_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_parttime ci_upper_parttime ci_lower_parttime)
tempfile parttime
save `parttime'

use "temp/gss_fulltime_controls.dta"
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `parttime'
	rename (_margin _ci_lb _ci_ub) (dydx_fulltime ci_lower_fulltime ci_upper_fulltime)
	keep year dydx* ci_lower* ci_upper*
	gen year_parttime=year+.3
	replace year=year-.3
	rename year year_fulltime
	set obs 22
	replace dydx_parttime=0 in 22
	replace dydx_fulltime=0 in 22
	replace ci_upper_parttime=0 in 22
	replace ci_lower_parttime=0 in 22
	replace ci_upper_fulltime=0 in 22
	replace ci_lower_fulltime=0 in 22
	replace year_fulltime=2001.7 in 22
	replace year_parttime=2002.3 in 22
	sort year_parttime

save "temp/gss_parttime.dta", replace
	
twoway (scatter dydx_parttime year_parttime, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_fulltime year_fulltime, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_parttime ci_lower_parttime year_parttime, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_fulltime ci_lower_fulltime year_fulltime, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Part-time") label(2 "Full-time") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "US 1977-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$usgraphics/gss_parttime_controls.png", as(png) name("Graph") replace

// cap erase "temp/gss_fulltime_controls.dta"
// cap erase "temp/gss_parttime_controls.dta"
// cap erase "temp/gss_fulltime_nocontrols.dta"
// cap erase "temp/gss_parttime_nocontrols.dta"

**Educated vs non-educated
use "clean/gss_clean.dta", clear

*With controls
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&uni==1) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_educated_controls.dta, replace)
	
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample&uni==0) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_uneducated_controls.dta, replace)

*Produce plot with controls	
use "temp/gss_educated_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_educated ci_upper_educated ci_lower_educated)
tempfile educated
save `educated'

use "temp/gss_uneducated_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `educated'	
	rename (_margin _ci_lb _ci_ub) (dydx_uneducated ci_lower_uneducated ci_upper_uneducated)
	keep year dydx* ci_lower* ci_upper*
	gen year_educated=year+.3
	replace year=year-.3
	rename year year_uneducated
	set obs 22
	replace dydx_educated=0 in 22
	replace dydx_uneducated=0 in 22
	replace ci_upper_educated=0 in 22
	replace ci_lower_educated=0 in 22
	replace ci_upper_uneducated=0 in 22
	replace ci_lower_uneducated=0 in 22
	replace year_uneducated=2001.7 in 22
	replace year_educated=2002.3 in 22
	sort year_educated
	
save "temp/gss_educ.dta", replace	
	
twoway (scatter dydx_educated year_educated, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_uneducated year_uneducated, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_educated ci_lower_educated year_educated, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_uneducated ci_lower_uneducated year_uneducated, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "University degree") label(2 "No degree") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "US 1977-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$usgraphics/gss_education_controls.png", as(png) name("Graph") replace

// cap erase "temp/gss_educated_controls.dta"
// cap erase "temp/gss_uneducated_controls.dta"
// cap erase "temp/gss_educated_nocontrols.dta"
// cap erase "temp/gss_uneducated_nocontrols.dta"

**Self-employed
use "clean/gss_clean.dta", clear	
	drop if year<1977
	drop if year==1980|year==1984|year==1987
	foreach var in employed semp year onedigit_naics age age_squared male education nonwhite union marital {
			drop if `var'>=.
	}
	
*With controls
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.nonwhite i.immigrant i.union ib5.marital if (insample&semp==1) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_semp_controls.dta, replace)
	
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.uni i.nonwhite i.immigrant i.union ib5.marital if (insample&semp==0) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/gss_nonsemp_controls.dta, replace)	

*Produce plot with controls	
use "temp/gss_semp_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_semp ci_upper_semp ci_lower_semp)
tempfile semp
save `semp'

use "temp/gss_nonsemp_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `semp'	
	rename (_margin _ci_lb _ci_ub) (dydx_nonsemp ci_lower_nonsemp ci_upper_nonsemp)
	keep year dydx* ci_lower* ci_upper*
	gen year_semp=year+.3
	replace year=year-.3
	rename year year_nonsemp
	set obs 22
	replace dydx_semp=0 in 22
	replace dydx_nonsemp=0 in 22
	replace ci_upper_semp=0 in 22
	replace ci_lower_semp=0 in 22
	replace ci_upper_nonsemp=0 in 22
	replace ci_lower_nonsemp=0 in 22
	replace year_nonsemp=2001.7 in 22
	replace year_semp=2002.3 in 22
	sort year_semp
	
save "temp/gss_semp.dta", replace
	
twoway (scatter dydx_semp year_semp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_nonsemp year_nonsemp, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_semp ci_lower_semp year_semp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_nonsemp ci_lower_nonsemp year_nonsemp, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Self-employed") label(2 "Not self-employed") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "US 1977-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$usgraphics/gss_semp_controls.png", as(png) name("Graph") replace

////////////////////////////////////////////////////////////////////////////////
*PART TWO, v2: US (HRS)
////////////////////////////////////////////////////////////////////////////////

cd "$usdata"

*Dummy regression to store empty results
use "clean/hrs_clean.dta", clear
reghdfe insecurity ib2002.year if insample [pw=wtresp], vce(cluster year) noabsorb
estimates save hrs, replace

*Men vs. women
use "clean/hrs_clean.dta", clear
	
*With controls
reghdfe insecurity ib2002.year  i.parttime age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample==1&male==1) [pw=wtresp], vce(cluster year) noabsorb
	margins, dydx(i.year) saving(temp/hrs_male_controls.dta, replace)
	
reghdfe insecurity ib2002.year  i.parttime age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample==1&male==0) [pw=wtresp], vce(cluster year) noabsorb
	margins, dydx(i.year) saving(temp/hrs_female_controls.dta, replace)	

*Produce plot with controls	
use "temp/hrs_male_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_male ci_upper_male ci_lower_male)
tempfile male
save `male'

use "temp/hrs_female_controls.dta"
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `male'
	rename (_margin _ci_lb _ci_ub) (dydx_female ci_lower_female ci_upper_female)
	keep year dydx* ci_lower* ci_upper*
	gen year_male=year+.3
	replace year=year-.3
	rename year year_female
	set obs 11
	replace dydx_male=0 in 11
	replace dydx_female=0 in 11
	replace ci_upper_male=0 in 11
	replace ci_lower_male=0 in 11
	replace ci_upper_female=0 in 11
	replace ci_lower_female=0 in 11
	replace year_female=2001.7 in 11
	replace year_male=2002.3 in 11
	sort year_male
	
save "temp/hrs_sex.dta", replace
	
twoway (scatter dydx_male year_male, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_female year_female, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_male ci_lower_male year_male, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_female ci_lower_female year_female, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1994(2)2016, angle(45)) scale(.8) legend(label(1 "Male") label(2 "Female") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "US 1994-2016, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$usgraphics/hrs_sex_controls.png", as(png) name("Graph") replace

// cap erase "temp/hrs_male_controls.dta"
// cap erase "temp/hrs_female_controls.dta"
// cap erase "temp/hrs_male_nocontrols.dta"
// cap erase "temp/hrs_female_nocontrols.dta"


**Part-time vs. full-time
use "clean/hrs_clean.dta", clear	
	
*With controls
reghdfe insecurity ib2002.year  i.parttime age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample==1&parttime==1) [pw=wtresp], vce(cluster year) noabsorb
	margins, dydx(i.year) saving(temp/hrs_parttime_controls.dta, replace)
	
reghdfe insecurity ib2002.year  i.parttime age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample==1&parttime==0) [pw=wtresp], vce(cluster year) noabsorb
	margins, dydx(i.year) saving(temp/hrs_fulltime_controls.dta, replace)	

*Produce plot with controls	
use "temp/hrs_parttime_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_parttime ci_upper_parttime ci_lower_parttime)
tempfile parttime
save `parttime'

use "temp/hrs_fulltime_controls.dta"
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `parttime'
	rename (_margin _ci_lb _ci_ub) (dydx_fulltime ci_lower_fulltime ci_upper_fulltime)
	keep year dydx* ci_lower* ci_upper*
	gen year_parttime=year+.3
	replace year=year-.3
	rename year year_fulltime
	set obs 11
	replace dydx_parttime=0 in 11
	replace dydx_fulltime=0 in 11
	replace ci_upper_parttime=0 in 11
	replace ci_lower_parttime=0 in 11
	replace ci_upper_fulltime=0 in 11
	replace ci_lower_fulltime=0 in 11
	replace year_fulltime=2001.7 in 11
	replace year_parttime=2002.3 in 11
	sort year_parttime
	
save "temp/hrs_parttime.dta", replace
	
twoway (scatter dydx_parttime year_parttime, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_fulltime year_fulltime, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_parttime ci_lower_parttime year_parttime, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_fulltime ci_lower_fulltime year_fulltime, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1994(2)2016, angle(45)) scale(.8) legend(label(1 "Part-time") label(2 "Full-time") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "US 1994-2016, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$usgraphics/hrs_parttime_controls.png", as(png) name("Graph") replace

// cap erase "temp/hrs_fulltime_controls.dta"
// cap erase "temp/hrs_parttime_controls.dta"
// cap erase "temp/hrs_fulltime_nocontrols.dta"
// cap erase "temp/hrs_parttime_nocontrols.dta"

**Educated vs non-educated
use "clean/hrs_clean.dta", clear
	
*With controls
reghdfe insecurity ib2002.year  i.parttime age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample==1&uni==1) [pw=wtresp], vce(cluster year) noabsorb
	margins, dydx(i.year) saving(temp/hrs_educated_controls.dta, replace)
	
reghdfe insecurity ib2002.year  i.parttime age age_squared i.male i.uni i.union i.immigrant i.nonwhite ib5.marital if (insample==1&uni==0) [pw=wtresp], vce(cluster year) noabsorb
	margins, dydx(i.year) saving(temp/hrs_uneducated_controls.dta, replace)	

*Produce plot with controls	
use "temp/hrs_educated_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_educated ci_upper_educated ci_lower_educated)
tempfile educated
save `educated'

use "temp/hrs_uneducated_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `educated'	
	rename (_margin _ci_lb _ci_ub) (dydx_uneducated ci_lower_uneducated ci_upper_uneducated)
	keep year dydx* ci_lower* ci_upper*
	gen year_educated=year+.3
	replace year=year-.3
	rename year year_uneducated
	set obs 11
	replace dydx_educated=0 in 11
	replace dydx_uneducated=0 in 11
	replace ci_upper_educated=0 in 11
	replace ci_lower_educated=0 in 11
	replace ci_upper_uneducated=0 in 11
	replace ci_lower_uneducated=0 in 11
	replace year_uneducated=2001.7 in 11
	replace year_educated=2002.3 in 11
	sort year_educated
	
save "temp/hrs_educ.dta", replace
	
twoway (scatter dydx_educated year_educated, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_uneducated year_uneducated, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_educated ci_lower_educated year_educated, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_uneducated ci_lower_uneducated year_uneducated, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1994(2)2016, angle(45)) scale(.8) legend(label(1 "University degree") label(2 "No degree") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "US 1994-2016, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$usgraphics/hrs_education_controls.png", as(png) name("Graph") replace

// cap erase "temp/hrs_educated_controls.dta"
// cap erase "temp/hrs_uneducated_controls.dta"
// cap erase "temp/hrs_educated_nocontrols.dta"
// cap erase "temp/hrs_uneducated_nocontrols.dta"

////////////////////////////////////////////////////////////////////////////////
*PART THREE: Germany
////////////////////////////////////////////////////////////////////////////////

cd "$germanydata"

*Dummy regression to store empty estimates
use "clean/soep_clean.dta", clear

logit insecure ib2001.year if (insample==1) [pw=xw], vce(cluster year)
estimates save soep, replace

**New starters vs. non-new starters	
use "clean/soep_clean.dta", clear

*With controls
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&jobtenure<1 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_new_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&jobtenure>=1 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_old_controls.dta, replace)	

*Produce plot with controls	
use "temp/soep_new_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_new ci_upper_new ci_lower_new)
tempfile new
save `new'

use "temp/soep_old_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `new'	
	rename (_margin _ci_lb _ci_ub) (dydx_old ci_lower_old ci_upper_old)
	keep year dydx* ci_lower* ci_upper*
	gen year_new=year+.1
	replace year=year-.1
	rename year year_old
	set obs 34
	replace dydx_new=0 in 34
	replace dydx_old=0 in 34
	replace ci_upper_new=0 in 34
	replace ci_lower_new=0 in 34
	replace ci_upper_old=0 in 34
	replace ci_lower_old=0 in 34
	replace year_old=2000.9 in 34
	replace year_new=2001.1 in 34
	sort year_new
	
save "temp/soep_jobtenure.dta", replace
	
twoway (scatter dydx_new year_new, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_old year_old, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_new ci_lower_new year_new, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_old ci_lower_old year_old, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "New starters") label(2 "Non-new starters") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1984-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_new_controls.png", as(png) name("Graph") replace

// cap erase "temp/soep_new_nocontrols.dta"
// cap erase "temp/soep_old_nocontrols.dta"
// cap erase "temp/soep_new_controls.dta"
// cap erase "temp/soep_old_controls.dta"

** Over-50s vs. prime age (25-49) vs. under-25s
use "clean/soep_clean.dta", clear
	
*With controls
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&age>=50 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_over50_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&inrange(age,25,49) [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_primeage_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&age<25 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_under25_controls.dta, replace)	

*Produce plot with controls	
use "temp/soep_over50_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_over ci_upper_over ci_lower_over)
tempfile over
save `over'

use "temp/soep_primeage_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_primeage ci_upper_primeage ci_lower_primeage)
tempfile prime
save `prime'

use "temp/soep_under25_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `over', nogen
merge 1:1 year using `prime', nogen
	rename (_margin _ci_lb _ci_ub) (dydx_under ci_lower_under ci_upper_under)
	keep year dydx* ci_lower* ci_upper*
	gen year_over=year+.1
	gen year_under=year-.1
	rename year year_primeage
	set obs 34
	replace dydx_over=0 in 34
	replace dydx_under=0 in 34
	replace dydx_primeage=0 in 34
	replace ci_upper_over=0 in 34
	replace ci_lower_over=0 in 34
	replace ci_upper_primeage=0 in 34
	replace ci_lower_primeage=0 in 34
	replace ci_upper_under=0 in 34
	replace ci_lower_under=0 in 34
	replace year_under=2000.9 in 34
	replace year_primeage=2001 in 34
	replace year_over=2001.1 in 34
	sort year_over
	
save "temp/soep_age.dta", replace
	
twoway (scatter dydx_over year_over, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_primeage year_primeage, connect(direct) msize(small) lcolor(gs7) mcolor(gs7)) (scatter dydx_under year_under, connect(direct) msize(small) lcolor(gs12) mcolor(gs12)) (rcap ci_upper_over ci_lower_over year_over, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_primeage ci_lower_primeage year_primeage, msize(0) lcolor(gs7) mcolor(gs7)) (rcap ci_upper_under ci_lower_under year_under, msize(0) lcolor(gs12) mcolor(gs12)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Over 50") label(2 "25-49") label(3 "Under 25") label(4 "95% CI") label(5 "95% CI") label(6 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1984-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_age_controls.png", as(png) name("Graph") replace

// cap erase "temp/soep_over50_nocontrols.dta"
// cap erase "temp/soep_primeage_nocontrols.dta"
// cap erase "temp/soep_under25_nocontrols.dta"
// cap erase "temp/soep_over50_controls.dta"
// cap erase "temp/soep_primeage_controls.dta"
// cap erase "temp/soep_under25_controls.dta"

**Male vs. female
use "clean/soep_clean.dta", clear
	
*With controls
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&male==1 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_male_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&male==0 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_female_controls.dta, replace)	

*Produce plot with controls	
use "temp/soep_male_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_male ci_upper_male ci_lower_male)
tempfile male
save `male'

use "temp/soep_female_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `male'	
	rename (_margin _ci_lb _ci_ub) (dydx_female ci_lower_female ci_upper_female)
	keep year dydx* ci_lower* ci_upper*
	gen year_male=year+.1
	replace year=year-.1
	rename year year_female
	set obs 34
	replace dydx_male=0 in 34
	replace dydx_female=0 in 34
	replace ci_upper_male=0 in 34
	replace ci_lower_male=0 in 34
	replace ci_upper_female=0 in 34
	replace ci_lower_female=0 in 34
	replace year_female=2000.9 in 34
	replace year_male=2001.1 in 34
	sort year_male
	
save "temp/soep_sex.dta", replace
	
twoway (scatter dydx_male year_male, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_female year_female, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_male ci_lower_male year_male, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_female ci_lower_female year_female, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Male") label(2 "Female") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1984-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_sex_controls.png", as(png) name("Graph") replace

// cap erase "temp/soep_male_nocontrols.dta"
// cap erase "temp/soep_female_nocontrols.dta"
// cap erase "temp/soep_male_controls.dta"
// cap erase "temp/soep_female_controls.dta"

**Marginal vs. non-marginal employment
use "clean/soep_clean.dta", clear

*With controls
logit insecure ib2001.year i.ddr i.temp i.semp i.marginal i.parttime  i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&marginal==1 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_marginal_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.marginal i.parttime  i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&marginal==0 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_nonmarginal_controls.dta, replace)	

*Produce plot with controls	
use "temp/soep_marginal_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_marginal ci_upper_marginal ci_lower_marginal)
tempfile marginal
save `marginal'

use "temp/soep_nonmarginal_controls.dta", clear	
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `marginal'	
	rename (_margin _ci_lb _ci_ub) (dydx_nonmarginal ci_lower_nonmarginal ci_upper_nonmarginal)
	keep year dydx* ci_lower* ci_upper*
	gen year_marginal=year+.1
	replace year=year-.1
	rename year year_nonmarginal
	set obs 34
	replace dydx_marginal=0 in 34
	replace dydx_nonmarginal=0 in 34
	replace ci_upper_marginal=0 in 34
	replace ci_lower_marginal=0 in 34
	replace ci_upper_nonmarginal=0 in 34
	replace ci_lower_nonmarginal=0 in 34
	replace year_nonmarginal=2000.9 in 34
	replace year_marginal=2001.1 in 34
	sort year_marginal

save "temp/soep_marginal.dta", replace
	
twoway (scatter dydx_marginal year_marginal, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_nonmarginal year_nonmarginal, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_marginal ci_lower_marginal year_marginal, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_nonmarginal ci_lower_nonmarginal year_nonmarginal, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Marginal employment") label(2 "Non-marginal employment") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1984-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_marginal_controls.png", as(png) name("Graph") replace

// cap erase "temp/soep_marginal_nocontrols.dta"
// cap erase "temp/soep_nonmarginal_nocontrols.dta"
// cap erase "temp/soep_marginal_controls.dta"
// cap erase "temp/soep_nonmarginal_controls.dta"

**Educated vs. non-educated
use "clean/soep_clean.dta", clear
	drop if education<0|education>=.

*With controls
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&uni==1 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_educated_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&uni==0 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_uneducated_controls.dta, replace)	

*Produce plot with controls	
use "temp/soep_educated_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_educated ci_upper_educated ci_lower_educated)
tempfile educated
save `educated'

use "temp/soep_uneducated_controls.dta", clear	
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `educated'	
	rename (_margin _ci_lb _ci_ub) (dydx_uneducated ci_lower_uneducated ci_upper_uneducated)
	keep year dydx* ci_lower* ci_upper*
	gen year_educated=year+.1
	replace year=year-.1
	rename year year_uneducated
	set obs 34
	replace dydx_educated=0 in 34
	replace dydx_uneducated=0 in 34
	replace ci_upper_educated=0 in 34
	replace ci_lower_educated=0 in 34
	replace ci_upper_uneducated=0 in 34
	replace ci_lower_uneducated=0 in 34
	replace year_uneducated=2000.9 in 34
	replace year_educated=2001.1 in 34
	sort year_educated

save "temp/soep_educ.dta", replace	
	
twoway (scatter dydx_educated year_educated, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_uneducated year_uneducated, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_educated ci_lower_educated year_educated, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_uneducated ci_lower_uneducated year_uneducated, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Tertiary degree") label(2 "No degree") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1984-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_education_controls.png", as(png) name("Graph") replace

// cap erase "temp/soep_educated_nocontrols.dta"
// cap erase "temp/soep_uneducated_nocontrols.dta"
// cap erase "temp/soep_educated_controls.dta"
// cap erase "temp/soep_uneducated_controls.dta"

**Part-time vs. full-time
use "clean/soep_clean.dta", clear
	
*With controls
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&parttime==1 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_parttime_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&parttime==0 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_fulltime_controls.dta, replace)	

*Produce plot with controls	
use "temp/soep_parttime_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_parttime ci_upper_parttime ci_lower_parttime)
tempfile parttime
save `parttime'

use "temp/soep_fulltime_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `parttime'	
	rename (_margin _ci_lb _ci_ub) (dydx_fulltime ci_lower_fulltime ci_upper_fulltime)
	keep year dydx* ci_lower* ci_upper*
	gen year_parttime=year+.1
	replace year=year-.1
	rename year year_fulltime
	set obs 34
	replace dydx_parttime=0 in 34
	replace dydx_fulltime=0 in 34
	replace ci_upper_parttime=0 in 34
	replace ci_lower_parttime=0 in 34
	replace ci_upper_fulltime=0 in 34
	replace ci_lower_fulltime=0 in 34
	replace year_fulltime=2000.9 in 34
	replace year_parttime=2001.1 in 34
	sort year_parttime
	
save "temp/soep_parttime.dta", replace
	
twoway (scatter dydx_parttime year_parttime, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_fulltime year_fulltime, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_parttime ci_lower_parttime year_parttime, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_fulltime ci_lower_fulltime year_fulltime, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Part-time") label(2 "Full-time") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1984-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_parttime_controls.png", as(png) name("Graph") replace

// cap erase "temp/soep_fulltime_nocontrols.dta"
// cap erase "temp/soep_parttime_nocontrols.dta"
// cap erase "temp/soep_fulltime_controls.dta"
// cap erase "temp/soep_parttime_controls.dta"

**Temporary vs. permanent workers
use "clean/soep_clean.dta", clear
	
*With controls
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&temp==1 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_temp_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&temp==0 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_perm_controls.dta, replace)	

*Produce plot with controls	
use "temp/soep_temp_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_temp ci_upper_temp ci_lower_temp)
tempfile temp
save `temp'
	
use "temp/soep_perm_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `temp'	
	rename (_margin _ci_lb _ci_ub) (dydx_perm ci_lower_perm ci_upper_perm)
	keep year dydx* ci_lower* ci_upper*
	gen year_temp=year+.1
	replace year=year-.1
	rename year year_perm
	set obs 34
	replace dydx_temp=0 in 34
	replace dydx_perm=0 in 34
	replace ci_upper_temp=0 in 34
	replace ci_lower_temp=0 in 34
	replace ci_upper_perm=0 in 34
	replace ci_lower_perm=0 in 34
	replace year_perm=2000.9 in 34
	replace year_temp=2001.1 in 34
	sort year_temp
	
save "temp/soep_temp.dta", replace
	
twoway (scatter dydx_temp year_temp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_perm year_perm, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_temp ci_lower_temp year_temp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_perm ci_lower_perm year_perm, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Temporary workers") label(2 "Permanent workers") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1984-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_temp_controls.png", as(png) name("Graph") replace

// cap erase "temp/soep_perm_nocontrols.dta"
// cap erase "temp/soep_temp_nocontrols.dta"
// cap erase "temp/soep_perm_controls.dta"
// cap erase "temp/soep_temp_controls.dta"

**Self-employed vs. normal workers
use "clean/soep_clean.dta", clear
	
*With controls
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&semp==1 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_semp_controls.dta, replace)
	
logit insecure ib2001.year i.ddr i.temp i.semp i.parttime i.marginal i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.uni if insample==1&semp==0 [pw=xw], vce(cluster year)	
	margins, dydx(i.year) saving(temp/soep_nonsemp_controls.dta, replace)	

*Produce plot with controls	
use "temp/soep_semp_controls.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx_semp ci_upper_semp ci_lower_semp)
tempfile semp
save `semp'

use "temp/soep_nonsemp_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `semp'	
	rename (_margin _ci_lb _ci_ub) (dydx_nonsemp ci_lower_nonsemp ci_upper_nonsemp)
	keep year dydx* ci_lower* ci_upper*
	gen year_semp=year+.1
	replace year=year-.1
	rename year year_nonsemp
	set obs 34
	replace dydx_semp=0 in 34
	replace dydx_nonsemp=0 in 34
	replace ci_upper_semp=0 in 34
	replace ci_lower_semp=0 in 34
	replace ci_upper_nonsemp=0 in 34
	replace ci_lower_nonsemp=0 in 34
	replace year_nonsemp=2000.9 in 34
	replace year_semp=2001.1 in 34
	sort year_semp
	
save "temp/soep_semp.dta", replace
	
twoway (scatter dydx_semp year_semp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_nonsemp year_nonsemp, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_semp ci_lower_semp year_semp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_nonsemp ci_lower_nonsemp year_nonsemp, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Self-employed") label(2 "Not self-employed") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "Germany 1984-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_semp_controls.png", as(png) name("Graph") replace

// cap erase "temp/soep_semp_nocontrols.dta"
// cap erase "temp/soep_nonsemp_nocontrols.dta"
// cap erase "temp/soep_semp_controls.dta"
// cap erase "temp/soep_nonsemp_controls.dta"

////////////////////////////////////////////////////////////////////////////////
*Step 2: regress year dummy mfx on unemployment and time trend
////////////////////////////////////////////////////////////////////////////////

**PART ONE: UK

cd "$ukdata"

** Male vs. female
use "temp/male_controls.dta", clear
	gen dydx_male=dprob1dx_controls+dprob2dx_controls
	keep dydx_male year
merge 1:1 year using "temp/female_controls.dta", keepusing(dprob1dx_controls dprob2dx_controls)
	gen dydx_female=dprob1dx_controls+dprob2dx_controls
	keep dydx* year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx_male=dydx_male*100
replace dydx_female=dydx_female*100

*Regressions
reg dydx_male c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_male.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_male.dta, replace)

reg dydx_female c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_female.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_female.dta, replace)

**Male vs. female, w/ easier t-test for group difference
use "temp/male_controls.dta", clear
	gen male=1
append using "temp/female_controls.dta"
	recode male .=0
	gen dydx=dprob1dx_controls+dprob2dx_controls
	keep dydx* male year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_male = male*t
gen unrate_male=male*unrate
replace t=0 if male
replace unrate=0 if male

reg dydx i.male c.t c.unrate c.t_male c.unrate_male, vce(robust)
test t=t_male
scalar def p_sex = `r(p)'

** Over-50 vs. prime age vs. under-25
use "temp/over50_controls.dta", clear
	gen dydx_over=dprob1dx_controls+dprob2dx_controls
	keep dydx_over year
merge 1:1 year using "temp/primeage_controls.dta", keepusing(dprob1dx_controls dprob2dx_controls)
	gen dydx_primeage = dprob1dx_controls+dprob2dx_controls
	keep dydx* year
merge 1:1 year using "temp/under25_controls.dta", keepusing(dprob1dx_controls dprob2dx_controls)
	gen dydx_under=dprob1dx_controls+dprob2dx_controls
	keep dydx* year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx_over=dydx_over*100
replace dydx_primeage=dydx_primeage*100
replace dydx_under=dydx_under*100

*Regressions
reg dydx_over c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_over50.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_over50.dta, replace)

reg dydx_primeage c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_primeage.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_primeage.dta, replace)

reg dydx_under c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_under25.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_under25.dta, replace)

**Over-50 vs. prime age vs. under-25, w/ easier t-test for group difference
use "temp/over50_controls.dta", clear
	gen over=1
append using "temp/primeage_controls.dta"
	recode over .=0
	gen primeage=1 if over==0
append using "temp/under25_controls.dta"
	gen under=1 if over==.
	recode over .=0
	recode under .=0
	recode primeage .=0
	gen dydx=dprob1dx_controls+dprob2dx_controls
	keep dydx* over primeage under year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_over = over*t
gen unrate_over=over*unrate
gen t_under = under*t
gen unrate_under = under*unrate
gen t_primeage = primeage*t
gen unrate_primeage = primeage*unrate

reg dydx i.over i.primeage i.under c.t_primeage c.unrate_primeage c.t_over c.unrate_over c.t_under c.unrate_under, vce(robust) nocons
test t_primeage=t_over=t_under
scalar def p_age = `r(p)'

** Educated vs. uneducated
use "temp/educated_controls.dta", clear
	gen dydx_educated=dprob1dx_controls+dprob2dx_controls
	keep dydx_educated year
merge 1:1 year using "temp/uneducated_controls.dta", keepusing(dprob1dx_controls dprob2dx_controls)
	gen dydx_uneducated=dprob1dx_controls+dprob2dx_controls
	keep dydx* year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx_educated=dydx_educated*100
replace dydx_uneducated=dydx_uneducated*100

*Regressions
reg dydx_educated c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_educated.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_educated.dta, replace)

reg dydx_uneducated c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_uneducated.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_uneducated.dta, replace)

**Educated vs. uneducated, w/ easier t-test for group difference
use "temp/educated_controls.dta", clear
	gen ed=1
append using "temp/uneducated_controls.dta"
	recode ed .=0
	gen dydx=dprob1dx_controls+dprob2dx_controls
	keep dydx* ed year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_ed = ed*t
gen unrate_ed=ed*unrate
replace t=0 if ed
replace unrate=0 if ed

reg dydx i.ed c.t c.unrate c.t_ed c.unrate_ed, vce(robust)
test t=t_ed
scalar def p_education = `r(p)'

*New starters vs. non-new starters
use "temp/new_controls.dta", clear
	gen dydx_new=dprob1dx_controls+dprob2dx_controls
	keep dydx_new year
merge 1:1 year using "temp/old_controls.dta", keepusing(dprob1dx_controls dprob2dx_controls)
	gen dydx_old=dprob1dx_controls+dprob2dx_controls
	keep dydx* year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx_new=dydx_new*100
replace dydx_old=dydx_old*100

*Regressions
reg dydx_new c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_new.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_new.dta, replace)

reg dydx_old c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_old.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_old.dta, replace)

**New starters vs. non-new starters, w/ easier t-test for group difference
use "temp/new_controls.dta", clear
	gen new=1
append using "temp/old_controls.dta"
	recode new .=0
	gen dydx=dprob1dx_controls+dprob2dx_controls
	keep dydx* new year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_new = new*t
gen unrate_new=new*unrate
replace t=0 if new
replace unrate=0 if new

reg dydx i.new c.t c.unrate c.t_new c.unrate_new, vce(robust)
test t=t_new
scalar def p_tenure = `r(p)'

*Temporary vs permanent workers
use "temp/temp_controls.dta", clear
	gen dydx_temp=dprob1dx_controls+dprob2dx_controls
	keep dydx_temp year
merge 1:1 year using "temp/perm_controls.dta", keepusing(dprob1dx_controls dprob2dx_controls)
	gen dydx_perm=dprob1dx_controls+dprob2dx_controls
	keep dydx* year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx_temp=dydx_temp*100
replace dydx_perm=dydx_perm*100

*Regressions
reg dydx_temp c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_temp.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_temp.dta, replace)

reg dydx_perm c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_perm.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_perm.dta, replace)

**Temporary vs. permanent workers, w/ easier t-test for group difference
use "temp/temp_controls.dta", clear
	gen temp=1
append using "temp/perm_controls.dta"
	recode temp .=0
	gen dydx=dprob1dx_controls+dprob2dx_controls
	keep dydx* temp year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_temp = temp*t
gen unrate_temp=temp*unrate
replace t=0 if temp
replace unrate=0 if temp

reg dydx i.temp c.t c.unrate c.t_temp c.unrate_temp, vce(robust)
test t=t_temp
scalar def p_temp = `r(p)'

*Part-time vs. full-time
use "temp/parttime_controls.dta", clear
	gen dydx_parttime=dprob1dx_controls+dprob2dx_controls
	keep dydx_parttime year
merge 1:1 year using "temp/fulltime_controls.dta", keepusing(dprob1dx_controls dprob2dx_controls)
	gen dydx_fulltime=dprob1dx_controls+dprob2dx_controls
	keep dydx* year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx_parttime=dydx_parttime*100
replace dydx_fulltime=dydx_fulltime*100

*Regressions
reg dydx_parttime c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_parttime.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_parttime.dta, replace)

reg dydx_fulltime c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_fulltime.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_fulltime.dta, replace)

**Part-time vs. full-time workers, w/ easier t-test for group difference
use "temp/parttime_controls.dta", clear
	gen pt=1
append using "temp/fulltime_controls.dta"
	recode pt .=0
	gen dydx=dprob1dx_controls+dprob2dx_controls
	keep dydx* pt year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_pt = pt*t
gen unrate_pt=pt*unrate
replace t=0 if pt
replace unrate=0 if pt

reg dydx i.pt c.t c.unrate c.t_pt c.unrate_pt, vce(robust)
test t=t_pt
scalar def p_parttime = `r(p)'

*Self-employed vs. non-self-employed
use "temp/semp_controls.dta", clear
	gen dydx_semp=dprob1dx_controls+dprob2dx_controls
	keep dydx_semp year
merge 1:1 year using "temp/nonsemp_controls.dta", keepusing(dprob1dx_controls dprob2dx_controls)
	gen dydx_nonsemp=dprob1dx_controls+dprob2dx_controls
	keep dydx* year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx_semp=dydx_semp*100
replace dydx_nonsemp=dydx_nonsemp*100

*Regressions
reg dydx_semp c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_semp.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_semp.dta, replace)

reg dydx_nonsemp c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/t_ame_nonsemp.dta, replace)
margins, dydx(unrate) saving (temp/unrate_ame_nonsemp.dta, replace)

**Self-employed vs. non-self-employed, w/ easier t-test for group difference
use "temp/semp_controls.dta", clear
	gen semp=1
append using "temp/nonsemp_controls.dta"
	recode semp .=0
	gen dydx=dprob1dx_controls+dprob2dx_controls
	keep dydx* semp year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1991
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_semp = semp*t
gen unrate_semp=semp*unrate
replace t=0 if semp
replace unrate=0 if semp

reg dydx i.semp c.t c.unrate c.t_semp c.unrate_semp, vce(robust)
test t=t_semp
scalar def p_semp = `r(p)'

**Put AMEs into ereturn of estimates to then turn into tables

cd "$ukdata"

*First put unemployment rate AMEs into estimates
use "temp/unrate_ame_new.dta", clear
append using "temp/unrate_ame_old.dta"
append using "temp/unrate_ame_over50.dta"
append using "temp/unrate_ame_primeage.dta"
append using "temp/unrate_ame_under25.dta"
append using "temp/unrate_ame_male.dta"
append using "temp/unrate_ame_female.dta"
append using "temp/unrate_ame_educated.dta"
append using "temp/unrate_ame_uneducated.dta"
append using "temp/unrate_ame_temp.dta"
append using "temp/unrate_ame_perm.dta"
append using "temp/unrate_ame_parttime.dta"
append using "temp/unrate_ame_fulltime.dta"
append using "temp/unrate_ame_semp.dta"
append using "temp/unrate_ame_nonsemp.dta"

rename (_margin _statistic _se _pvalue) (ame z se pvalue)

mkmat ame, matrix(m)
mat m = m'
mat colnames m = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp

mkmat se, matrix(s)
mat s = s'
mat colnames s = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp

mkmat pvalue, matrix(pvalue)
mat pvalue = pvalue'
mat colnames pvalue = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp

estimates use bhps

estadd matrix margins_b = m
estadd matrix margins_se = s
estadd matrix ps = pvalue
estadd scalar margins_N = 27
estimates store bhps_unrate

*Now put time trend AMEs into estimates
use "temp/t_ame_new.dta", clear
append using "temp/t_ame_old.dta"
append using "temp/t_ame_over50.dta"
append using "temp/t_ame_primeage.dta"
append using "temp/t_ame_under25.dta"
append using "temp/t_ame_male.dta"
append using "temp/t_ame_female.dta"
append using "temp/t_ame_educated.dta"
append using "temp/t_ame_uneducated.dta"
append using "temp/t_ame_temp.dta"
append using "temp/t_ame_perm.dta"
append using "temp/t_ame_parttime.dta"
append using "temp/t_ame_fulltime.dta"
append using "temp/t_ame_semp.dta"
append using "temp/t_ame_nonsemp.dta"

rename (_margin _statistic _se _pvalue) (ame z se pvalue)

gen joint_p=.
replace joint_p = p_tenure in 1/2
replace joint_p = p_age in 3/5
replace joint_p = p_sex in 6/7
replace joint_p = p_education in 8/9
replace joint_p = p_temp in 10/11
replace joint_p = p_parttime in 12/13
replace joint_p = p_semp in 14/15

mkmat ame, matrix(m)
mat m = m'
mat colnames m = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp

mkmat se, matrix(s)
mat s = s'
mat colnames s = newstarters nonnewstarters over50 primeage under25 male female degree nodegree ///
temp perm parttime fulltime semp nonsemp

mkmat pvalue, matrix(pvalue)
mat pvalue = pvalue'
mat colnames pvalue = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp

mkmat joint_p, matrix(joint_p)
mat joint_p = joint_p'
mat colnames joint_p = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp

estimates use bhps

estadd matrix margins_b = m
estadd matrix margins_se = s
estadd matrix ps = pvalue
estadd matrix joint_p = joint_p
estadd scalar margins_N = 27
estimates store bhps_t

**PART TWO, v1: US (GSS)

cd "$usdata"

** Male vs. female
use "temp/gss_male_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/gss_female_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_male dydx_female)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	// rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx_male=dydx_male*100
replace dydx_female=dydx_female*100

*Regressions
reg dydx_male c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_male.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_male.dta, replace)

reg dydx_female c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_female.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_female.dta, replace)

**Male vs. female, w/ easier t-test for group difference
use "temp/gss_male_controls.dta", clear
	gen male=1
append using "temp/gss_female_controls.dta"
	recode male .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* male year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_male = male*t
gen unrate_male=male*unrate
replace t=0 if male
replace unrate=0 if male

reg dydx i.male c.t c.unrate c.t_male c.unrate_male, vce(robust)
test t=t_male
scalar def p_sex = `r(p)'

** Under-50 vs. prime age vs. over-50
use "temp/gss_over50_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/gss_under25_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_over dydx_under)
	keep _deriv dydx*
merge 1:1 _deriv using "temp/gss_primeage_controls.dta", keepusing(_margin)
	rename _margin dydx_primeage
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv _merge

merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	// rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx_over=dydx_over*100
replace dydx_under=dydx_under*100
replace dydx_primeage=dydx_primeage*100

*Regressions
reg dydx_over c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_over50.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_over50.dta, replace)

reg dydx_primeage c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_primeage.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_primeage.dta, replace)

reg dydx_under c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_under25.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_under25.dta, replace)

**Over- vs. under-50s, w/ easier t-test for group difference
use "temp/gss_over50_controls.dta", clear
	gen over=1
append using "temp/gss_under25_controls.dta"
	gen under=(over==.)
	recode over .=0
append using "temp/gss_primeage_controls.dta"
	gen primeage=1 if over==.
	recode over .=0
	recode under .=0
	recode primeage .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* over under primeage year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_over = over*t
gen unrate_over=over*unrate
gen t_under = under*t
gen unrate_under=under*unrate
gen t_primeage = primeage*t
gen unrate_primeage=primeage*unrate

reg dydx i.over i.under i.primeage c.t_primeage c.unrate_primeage c.t_under c.unrate_under c.t_over c.unrate_over, vce(robust) nocons
test t_primeage=t_over=t_under
scalar def p_age = `r(p)'

** Educated vs. uneducated
use "temp/gss_educated_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/gss_uneducated_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_educated dydx_uneducated)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	// rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx_educated=dydx_educated*100
replace dydx_uneducated=dydx_uneducated*100

*Regressions
reg dydx_educated c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_educated.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_educated.dta, replace)

reg dydx_uneducated c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_uneducated.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_uneducated.dta, replace)

**Educated vs. uneducated, w/ easier t-test for group difference
use "temp/gss_educated_controls.dta", clear
	gen ed=1
append using "temp/gss_uneducated_controls.dta"
	recode ed .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* ed year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_ed = ed*t
gen unrate_ed=ed*unrate
replace t=0 if ed
replace unrate=0 if ed

reg dydx i.ed c.t c.unrate c.t_ed c.unrate_ed, vce(robust)
test t=t_ed
scalar def p_education = `r(p)'

*Part-time vs. full-time
use "temp/gss_parttime_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/gss_fulltime_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_parttime dydx_fulltime)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	// rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx_parttime=dydx_parttime*100
replace dydx_fulltime=dydx_fulltime*100

*Regressions
reg dydx_parttime c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_parttime.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_parttime.dta, replace)

reg dydx_fulltime c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_fulltime.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_fulltime.dta, replace)

**Part-time vs. full-time workers, w/ easier t-test for group difference
use "temp/gss_parttime_controls.dta", clear
	gen pt=1
append using "temp/gss_fulltime_controls.dta"
	recode pt .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* pt year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_pt = pt*t
gen unrate_pt=pt*unrate
replace t=0 if pt
replace unrate=0 if pt

reg dydx i.pt c.t c.unrate c.t_pt c.unrate_pt, vce(robust)
test t=t_pt
scalar def p_parttime = `r(p)'

*Self-employed vs. non-self-employed
use "temp/gss_semp_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/gss_nonsemp_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_semp dydx_nonsemp)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	// rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx_semp=dydx_semp*100
replace dydx_nonsemp=dydx_nonsemp*100

*Regressions
reg dydx_semp c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_semp.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_semp.dta, replace)

reg dydx_nonsemp c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/gss_t_ame_nonsemp.dta, replace)
margins, dydx(unrate) saving (temp/gss_unrate_ame_nonsemp.dta, replace)

**Self-employed vs. non-self-employed, w/ easier t-test for group difference
use "temp/gss_semp_controls.dta", clear
	gen semp=1
append using "temp/gss_nonsemp_controls.dta"
	recode semp .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* semp year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1978
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_semp = semp*t
gen unrate_semp=semp*unrate
replace t=0 if semp
replace unrate=0 if semp

reg dydx i.semp c.t c.unrate c.t_semp c.unrate_semp, vce(robust)
test t=t_semp
scalar def p_semp = `r(p)'

**Put AMEs into ereturn of estimates to then turn into tables

cd "$usdata"

*First put unemployment rate AMEs into estimates
use "temp/gss_unrate_ame_male.dta", clear
append using "temp/gss_unrate_ame_female.dta"
append using "temp/gss_unrate_ame_over50.dta"
append using "temp/gss_unrate_ame_primeage.dta"
append using "temp/gss_unrate_ame_under25.dta"
append using "temp/gss_unrate_ame_educated.dta"
append using "temp/gss_unrate_ame_uneducated.dta"
append using "temp/gss_unrate_ame_parttime.dta"
append using "temp/gss_unrate_ame_fulltime.dta"
append using "temp/gss_unrate_ame_semp.dta"
append using "temp/gss_unrate_ame_nonsemp.dta"

rename (_margin _statistic _se _pvalue) (ame z se pvalue)

mkmat ame, matrix(m)
mat m = m'
mat colnames m = male female over50 primeage under25 degree nodegree parttime fulltime semp nonsemp

mkmat se, matrix(s)
mat s = s'
mat colnames s = male female over50 primeage under25 degree nodegree parttime fulltime semp nonsemp

mkmat pvalue, matrix(pvalue)
mat pvalue = pvalue'
mat colnames pvalue = male female over50 primeage under25 degree nodegree parttime fulltime semp nonsemp

estimates use gss

estadd matrix margins_b = m
estadd matrix margins_se = s
estadd matrix ps = pvalue
estadd scalar margins_N = 21
estimates store gss_unrate

*Now put time trend AMEs into estimates
use "temp/gss_t_ame_male.dta", clear
append using "temp/gss_t_ame_female.dta"
append using "temp/gss_t_ame_over50.dta"
append using "temp/gss_t_ame_primeage.dta"
append using "temp/gss_t_ame_under25.dta"
append using "temp/gss_t_ame_educated.dta"
append using "temp/gss_t_ame_uneducated.dta"
append using "temp/gss_t_ame_parttime.dta"
append using "temp/gss_t_ame_fulltime.dta"
append using "temp/gss_t_ame_semp.dta"
append using "temp/gss_t_ame_nonsemp.dta"

rename (_margin _statistic _se _pvalue) (ame z se pvalue)

gen joint_p=.
replace joint_p = p_sex in 1/2
replace joint_p = p_age in 3/5
replace joint_p = p_education in 6/7
replace joint_p = p_parttime in 8/9
replace joint_p = p_semp in 10/11

mkmat ame, matrix(m)
mat m = m'
mat colnames m = male female over50 primeage under25 degree nodegree parttime fulltime semp nonsemp

mkmat se, matrix(s)
mat s = s'
mat colnames s = male female over50 primeage under25 degree nodegree parttime fulltime semp nonsemp

mkmat pvalue, matrix(pvalue)
mat pvalue = pvalue'
mat colnames pvalue = male female over50 primeage under25 degree nodegree parttime fulltime semp nonsemp

mkmat joint_p, matrix(joint_p)
mat joint_p = joint_p'
mat colnames joint_p = male female over50 primeage under25 degree nodegree parttime fulltime semp nonsemp

estimates use gss

estadd matrix margins_b = m
estadd matrix margins_se = s
estadd matrix ps = pvalue
estadd matrix joint_p = joint_p
estadd scalar margins_N = 21
estimates store gss_t


**PART TWO, v2: US (HRS)

cd "$usdata"

** Male vs. female
use "temp/hrs_male_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/hrs_female_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_male dydx_female)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	// rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1994
	
*Variable rescaling
replace t = t/10
replace dydx_male=dydx_male*100
replace dydx_female=dydx_female*100

*Regressions
reg dydx_male c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/hrs_t_ame_male.dta, replace)
margins, dydx(unrate) saving (temp/hrs_unrate_ame_male.dta, replace)

reg dydx_female c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/hrs_t_ame_female.dta, replace)
margins, dydx(unrate) saving (temp/hrs_unrate_ame_female.dta, replace)

**Male vs. female, w/ easier t-test for group difference
use "temp/hrs_male_controls.dta", clear
	gen male=1
append using "temp/hrs_female_controls.dta"
	recode male .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* male year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1994
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_male = male*t
gen unrate_male=male*unrate
replace t=0 if male
replace unrate=0 if male

reg dydx i.male c.t c.unrate c.t_male c.unrate_male, vce(robust)
test t=t_male
scalar def p_sex = `r(p)'

** Educated vs. uneducated
use "temp/hrs_educated_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/hrs_uneducated_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_educated dydx_uneducated)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	// rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1994
	
*Variable rescaling
replace t = t/10
replace dydx_educated=dydx_educated*100
replace dydx_uneducated=dydx_uneducated*100

*Regressions
reg dydx_educated c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/hrs_t_ame_educated.dta, replace)
margins, dydx(unrate) saving (temp/hrs_unrate_ame_educated.dta, replace)

reg dydx_uneducated c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/hrs_t_ame_uneducated.dta, replace)
margins, dydx(unrate) saving (temp/hrs_unrate_ame_uneducated.dta, replace)

**Educated vs. uneducated, w/ easier t-test for group difference
use "temp/hrs_educated_controls.dta", clear
	gen ed=1
append using "temp/hrs_uneducated_controls.dta"
	recode ed .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* ed year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1994
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_ed = ed*t
gen unrate_ed=ed*unrate
replace t=0 if ed
replace unrate=0 if ed

reg dydx i.ed c.t c.unrate c.t_ed c.unrate_ed, vce(robust)
test t=t_ed
scalar def p_education = `r(p)'

*Part-time vs. full-time
use "temp/hrs_parttime_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/hrs_fulltime_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_parttime dydx_fulltime)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	// rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1994
	
*Variable rescaling
replace t = t/10
replace dydx_parttime=dydx_parttime*100
replace dydx_fulltime=dydx_fulltime*100

*Regressions
reg dydx_parttime c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/hrs_t_ame_parttime.dta, replace)
margins, dydx(unrate) saving (temp/hrs_unrate_ame_parttime.dta, replace)

reg dydx_fulltime c.t c. unrate, vce(robust)
margins, dydx(t) saving(temp/hrs_t_ame_fulltime.dta, replace)
margins, dydx(unrate) saving (temp/hrs_unrate_ame_fulltime.dta, replace)

**Part-time vs. full-time workers, w/ easier t-test for group difference
use "temp/hrs_parttime_controls.dta", clear
	gen pt=1
append using "temp/hrs_fulltime_controls.dta"
	recode pt .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* pt year
	
merge m:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1994
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_pt = pt*t
gen unrate_pt=pt*unrate
replace t=0 if pt
replace unrate=0 if pt

reg dydx i.pt c.t c.unrate c.t_pt c.unrate_pt, vce(robust)
test t=t_pt
scalar def p_parttime = `r(p)'

**Put AMEs into ereturn of estimates to then turn into tables

cd "$usdata"

*First put unemployment rate AMEs into estimates
use "temp/hrs_unrate_ame_male.dta", clear
append using "temp/hrs_unrate_ame_female.dta"
append using "temp/hrs_unrate_ame_educated.dta"
append using "temp/hrs_unrate_ame_uneducated.dta"
append using "temp/hrs_unrate_ame_parttime.dta"
append using "temp/hrs_unrate_ame_fulltime.dta"

rename (_margin _statistic _se _pvalue) (ame z se pvalue)

mkmat ame, matrix(m)
mat m = m'
mat colnames m = male female degree nodegree parttime fulltime

mkmat se, matrix(s)
mat s = s'
mat colnames s = male female degree nodegree parttime fulltime

mkmat pvalue, matrix(pvalue)
mat pvalue = pvalue'
mat colnames pvalue = male female degree nodegree parttime fulltime

estimates use hrs

estadd matrix margins_b = m
estadd matrix margins_se = s
estadd matrix ps = pvalue
estadd scalar margins_N = 12
estimates store hrs_unrate

*Now put time trend AMEs into estimates
use "temp/hrs_t_ame_male.dta", clear
append using "temp/hrs_t_ame_female.dta"
append using "temp/hrs_t_ame_educated.dta"
append using "temp/hrs_t_ame_uneducated.dta"
append using "temp/hrs_t_ame_parttime.dta"
append using "temp/hrs_t_ame_fulltime.dta"

rename (_margin _statistic _se _pvalue) (ame z se pvalue)
gen joint_p=.
replace joint_p = p_sex in 1/2
replace joint_p = p_education in 3/4
replace joint_p = p_parttime in 5/6

mkmat ame, matrix(m)
mat m = m'
mat colnames m = male female degree nodegree parttime fulltime

mkmat se, matrix(s)
mat s = s'
mat colnames s = male female degree nodegree parttime fulltime

mkmat pvalue, matrix(pvalue)
mat pvalue = pvalue'
mat colnames pvalue = male female degree nodegree parttime fulltime

mkmat joint_p, matrix(joint_p)
mat joint_p = joint_p'
mat colnames joint_p = male female degree nodegree parttime fulltime

estimates use hrs

estadd matrix margins_b = m
estadd matrix margins_se = s
estadd matrix ps = pvalue
estadd matrix joint_p = joint_p
estadd scalar margins_N = 12
estimates store hrs_t

**PART THREE: Germany

cd "$germanydata"

** Male vs. female
use "temp/soep_male_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/soep_female_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_male dydx_female)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx_male=dydx_male*100
replace dydx_female=dydx_female*100

*Regressions
reg dydx_male t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_male.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_male.dta, replace)

reg dydx_female t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_female.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_female.dta, replace)

**Male vs. female, w/ easier t-test for group difference
use "temp/soep_male_controls.dta", clear
	gen male=1
append using "temp/soep_female_controls.dta"
	recode male .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* male year
	
merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_male = male*t
gen unrate_male=male*unrate
replace t=0 if male
replace unrate=0 if male

reg dydx i.male c.t c.unrate c.t_male c.unrate_male, vce(robust)
test t=t_male
scalar def p_sex = `r(p)'

** Over- vs. prime age (25-49) vs. under-50
use "temp/soep_over50_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/soep_under25_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_over dydx_under)
merge 1:1 _deriv using "temp/soep_primeage_controls.dta", keepusing(_margin)
rename _margin dydx_primeage
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv _merge

merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx_over=dydx_over*100
replace dydx_under=dydx_under*100
replace dydx_primeage=dydx_primeage*100

*Regressions
reg dydx_over t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_over50.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_over50.dta, replace)

reg dydx_primeage t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_primeage.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_primeage.dta, replace)

reg dydx_under t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_under25.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_under25.dta, replace)

**Over- vs. prime age (25-49) vs. under-25s, w/ easier t-test for group difference
use "temp/soep_over50_controls.dta", clear
	gen over=1
append using "temp/soep_under25_controls.dta"
	recode over .=0
	gen under=(over==0)
append using "temp/soep_primeage_controls.dta"
	gen primeage=1 if over==.
	recode over .=0
	recode primeage .=0
	recode under .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* over under primeage year
	
merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_over = over*t
gen unrate_over=over*unrate
gen t_under = under*t
gen unrate_under=under*unrate
gen t_primeage = primeage*t
gen unrate_primeage=primeage*unrate

reg dydx i.over i.under i.primeage c.t_under c.unrate_under c.t_over c.unrate_over c.t_primeage c.unrate_primeage, vce(robust) nocons
test t_under=t_over=t_primeage
scalar def p_age = `r(p)'

** Marginal vs. non-marginal employment
use "temp/soep_marginal_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/soep_nonmarginal_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_marginal dydx_nonmarginal)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx_marginal=dydx_marginal*100
replace dydx_nonmarginal=dydx_nonmarginal*100

*Regressions
reg dydx_marginal t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_marginal.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_marginal.dta, replace)

reg dydx_nonmarginal t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_nonmarginal.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_nonmarginal.dta, replace)

**Marginal vs. Nonmarginal, w/ easier t-test for group difference
use "temp/soep_marginal_controls.dta", clear
	gen marginal=1
append using "temp/soep_nonmarginal_controls.dta"
	recode marginal .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* marginal year
	
merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_marginal = marginal*t
gen unrate_marginal=marginal*unrate
replace t=0 if marginal
replace unrate=0 if marginal

reg dydx i.marginal c.t c.unrate c.t_marginal c.unrate_marginal, vce(robust)
test t=t_marginal
scalar def p_marginal = `r(p)'

** Educated vs. uneducated
use "temp/soep_educated_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/soep_uneducated_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_educated dydx_uneducated)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx_educated=dydx_educated*100
replace dydx_uneducated=dydx_uneducated*100

*Regressions
reg dydx_educated t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_educated.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_educated.dta, replace)

reg dydx_uneducated t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_uneducated.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_uneducated.dta, replace)

**Educated vs. uneducated, w/ easier t-test for group difference
use "temp/soep_educated_controls.dta", clear
	gen ed=1
append using "temp/soep_uneducated_controls.dta"
	recode ed .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* ed year
	
merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_ed = ed*t
gen unrate_ed=ed*unrate
replace t=0 if ed
replace unrate=0 if ed

reg dydx i.ed c.t c.unrate c.t_ed c.unrate_ed, vce(robust)
test t=t_ed
scalar def p_education = `r(p)'

*New starters vs. non-new starters
use "temp/soep_new_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/soep_old_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_new dydx_old)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx_new=dydx_new*100
replace dydx_old=dydx_old*100

*Regressions
reg dydx_new t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_new.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_new.dta, replace)

reg dydx_old t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_old.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_old.dta, replace)

**Mew starters vs. non-new starters, w/ easier t-test for group difference
use "temp/soep_new_controls.dta", clear
	gen new=1
append using "temp/soep_old_controls.dta"
	recode new .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* new year
	
merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_new = new*t
gen unrate_new=new*unrate
replace t=0 if new
replace unrate=0 if new

reg dydx i.new c.t c.unrate c.t_new c.unrate_new, vce(robust)
test t=t_new
scalar def p_tenure = `r(p)'

*Temporary vs permanent workers
use "temp/soep_temp_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/soep_perm_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_temp dydx_perm)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx_temp=dydx_temp*100
replace dydx_perm=dydx_perm*100

*Regressions
reg dydx_temp t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_temp.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_temp.dta, replace)

reg dydx_perm t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_perm.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_perm.dta, replace)

**Temporary vs. permanent workers, w/ easier t-test for group difference
use "temp/soep_temp_controls.dta", clear
	gen temp=1
append using "temp/soep_perm_controls.dta"
	recode temp .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* temp year
	
merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_temp = temp*t
gen unrate_temp=temp*unrate
replace t=0 if temp
replace unrate=0 if temp

reg dydx i.temp c.t c.unrate c.t_temp c.unrate_temp, vce(robust)
test t=t_temp
scalar def p_temp = `r(p)'

*Part-time vs. full-time
use "temp/soep_parttime_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/soep_fulltime_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_parttime dydx_fulltime)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx_parttime=dydx_parttime*100
replace dydx_fulltime=dydx_fulltime*100

*Regressions
reg dydx_parttime t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_parttime.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_parttime.dta, replace)

reg dydx_fulltime t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_fulltime.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_fulltime.dta, replace)

**Part-time vs. full-time workers, w/ easier t-test for group difference
use "temp/soep_parttime_controls.dta", clear
	gen pt=1
append using "temp/soep_fulltime_controls.dta"
	recode pt .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* pt year
	
merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_pt = pt*t
gen unrate_pt=pt*unrate
replace t=0 if pt
replace unrate=0 if pt

reg dydx i.pt c.t c.unrate c.t_pt c.unrate_pt, vce(robust)
test t=t_pt
scalar def p_parttime = `r(p)'

*Self-employed vs. non-self-employed
use "temp/soep_semp_controls.dta", clear
	rename _margin _margin1
	keep _deriv _margin1
merge 1:1 _deriv using "temp/soep_nonsemp_controls.dta", keepusing(_margin)
	drop _merge
rename (_margin1 _margin) (dydx_semp dydx_nonsemp)
decode _deriv, gen(year)
replace year=substr(year,1,4)
destring year, replace
drop _deriv

merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985

*Variable rescaling
replace t = t/10
replace dydx_semp=dydx_semp*100
replace dydx_nonsemp=dydx_nonsemp*100

*Regressions
reg dydx_semp t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_semp.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_semp.dta, replace)

reg dydx_nonsemp t unrate, vce(robust)
margins, dydx(t) saving(temp/soep_t_ame_nonsemp.dta, replace)
margins, dydx(unrate) saving (temp/soep_unrate_ame_nonsemp.dta, replace)

**Self-employed vs. non-self-employed, w/ easier t-test for group difference
use "temp/soep_semp_controls.dta", clear
	gen semp=1
append using "temp/soep_nonsemp_controls.dta"
	recode semp .=0
	rename _margin dydx
	decode _deriv, gen(year)
	replace year=substr(year,1,4)
	destring year, replace
	keep dydx* semp year
	
merge m:1 year using "clean/unemployment.dta"
	drop if _merge==2
	drop _merge
	rename urate unrate
	replace unrate=unrate*100
	
*Time trend variable
	gen t=year-1985
	
*Variable rescaling
replace t = t/10
replace dydx=dydx*100

gen t_semp = semp*t
gen unrate_semp=semp*unrate
replace t=0 if semp
replace unrate=0 if semp

reg dydx i.semp c.t c.unrate c.t_semp c.unrate_semp, vce(robust)
test t=t_semp
scalar def p_semp = `r(p)'

**Put AMEs into ereturn of estimates to then turn into tables

cd "$germanydata"

*First put unemployment rate AMEs into estimates
use "temp/soep_unrate_ame_new.dta", clear
append using "temp/soep_unrate_ame_old.dta"
append using "temp/soep_unrate_ame_over50.dta"
append using "temp/soep_unrate_ame_primeage.dta"
append using "temp/soep_unrate_ame_under25.dta"
append using "temp/soep_unrate_ame_male.dta"
append using "temp/soep_unrate_ame_female.dta"
append using "temp/soep_unrate_ame_educated.dta"
append using "temp/soep_unrate_ame_uneducated.dta"
append using "temp/soep_unrate_ame_temp.dta"
append using "temp/soep_unrate_ame_perm.dta"
append using "temp/soep_unrate_ame_parttime.dta"
append using "temp/soep_unrate_ame_fulltime.dta"
append using "temp/soep_unrate_ame_semp.dta"
append using "temp/soep_unrate_ame_nonsemp.dta"
append using "temp/soep_unrate_ame_marginal.dta"
append using "temp/soep_unrate_ame_nonmarginal.dta"

rename (_margin _statistic _se _pvalue) (ame z se pvalue)

mkmat ame, matrix(m)
mat m = m'
mat colnames m = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp marginal nonmarginal

mkmat se, matrix(s)
mat s = s'
mat colnames s = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp marginal nonmarginal

mkmat pvalue, matrix(pvalue)
mat pvalue = pvalue'
mat colnames pvalue = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp marginal nonmarginal

estimates use soep

estadd matrix margins_b = m
estadd matrix margins_se = s
estadd matrix ps = pvalue
estadd scalar margins_N = 34
estimates store soep_unrate

*Now put time trend AMEs into estimates
use "temp/soep_t_ame_new.dta", clear
append using "temp/soep_t_ame_old.dta"
append using "temp/soep_t_ame_over50.dta"
append using "temp/soep_t_ame_primeage.dta"
append using "temp/soep_t_ame_under25.dta"
append using "temp/soep_t_ame_male.dta"
append using "temp/soep_t_ame_female.dta"
append using "temp/soep_t_ame_educated.dta"
append using "temp/soep_t_ame_uneducated.dta"
append using "temp/soep_t_ame_temp.dta"
append using "temp/soep_t_ame_perm.dta"
append using "temp/soep_t_ame_parttime.dta"
append using "temp/soep_t_ame_fulltime.dta"
append using "temp/soep_t_ame_semp.dta"
append using "temp/soep_t_ame_nonsemp.dta"
append using "temp/soep_t_ame_marginal.dta"
append using "temp/soep_t_ame_nonmarginal.dta"

rename (_margin _statistic _se _pvalue) (ame z se pvalue)
gen joint_p=.
replace joint_p = p_tenure in 1/2
replace joint_p = p_age in 3/5
replace joint_p = p_sex in 6/7
replace joint_p = p_education in 8/9
replace joint_p = p_temp in 10/11
replace joint_p = p_parttime in 12/13
replace joint_p = p_semp in 14/15
replace joint_p = p_marginal in 16/17

mkmat ame, matrix(m)
mat m = m'
mat colnames m = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp marginal nonmarginal

mkmat se, matrix(s)
mat s = s'
mat colnames s = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp marginal nonmarginal

mkmat pvalue, matrix(pvalue)
mat pvalue = pvalue'
mat colnames pvalue = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp marginal nonmarginal

mkmat joint_p, matrix(joint_p)
mat joint_p = joint_p'
mat colnames joint_p = newstarters nonnewstarters over50 primeage under25 male female degree nodegree temp perm parttime fulltime semp nonsemp marginal nonmarginal

estimates use soep

estadd matrix margins_b = m
estadd matrix margins_se = s
estadd matrix ps = pvalue
estadd matrix joint_p = joint_p
estadd scalar margins_N = 34
estimates store soep_t

**Step 3: Make tables

cd "$tables"

////////////////////////////////////////////////////////////////////////////////
*TABLE 4: Coefficients on unemployment rate in heterogeneity regressions
////////////////////////////////////////////////////////////////////////////////

esttab gss_unrate hrs_unrate bhps_unrate soep_unrate using "unrate_ames_new.tex", replace cells(margins_b(fmt(%9.2f) star pvalue(ps)) margins_se(fmt(%9.2f) par(( )))) stats(margins_N, labels(N) fmt(0)) order(temp perm parttime fulltime newstarters nonnewstarters male female over50 primeage under25 degree nodegree marginal nonmarginal) title("Average marginal effect of unemployment rate*100, by subgroup") collabels("") mlabels("US (GSS)" "US (HRS)" "UK" "Germany") coeflabels(temp "Temporary workers" perm "Permanent workers" parttime "Part-time workers" fulltime "Full-time workers" newstarters "New starters" nonnewstarters "Non-new starters" male "Males" female "Females" degree "Tertiary degree" nodegree "No tertiary degree" over50 "Over 50 y.o." primeage "25-49" over25 "Over 25 y.o." under25 "Under 25 y.o." marginal "Marginally employed" nonmarginal "Non-marginally employed") nonumbers postfoot("\tabnotes{5}{This table displays the average marginal effect of the linear time trend in a linear regression, where the dependent variable is the AMEs of the year dummies from the main regression specification (Poisson for the UK, logistic for the GSS and SOEP, linear regression for the HRS) with the sample restricted to the subgroup of interest. Standard errors in parentheses. *\textit{p} < 0.05, **\textit{p} < 0.01, ***\textit{p} < 0.001}") width(\hsize) gaps booktab keep(temp perm parttime fulltime newstarters nonnewstarters male female degree nodegree over50 primeage under25 marginal nonmarginal)

////////////////////////////////////////////////////////////////////////////////
*TABLE 5: Coefficients on time trend in heterogeneity regressions
////////////////////////////////////////////////////////////////////////////////

esttab gss_t hrs_t bhps_t soep_t using "t_ames_new.tex", replace cells(margins_b(fmt(%9.2f) star pvalue(ps)) margins_se(fmt(%9.2f) par(( )))) stats(margins_N, labels(N) fmt(0)) order(temp perm parttime fulltime newstarters nonnewstarters male female over50 primeage under25 degree nodegree marginal nonmarginal) title("Average marginal effect of linear time trend, by subgroup") collabels("") mlabels("US (GSS)" "US (HRS)" "UK" "Germany") coeflabels(newstarters "New starters" nonnewstarters "Non-new starters" male "Males" female "Females" degree "Tertiary degree" nodegree "No tertiary degree" temp "Temporary workers" perm "Permanent workers" parttime "Part-time workers" fulltime "Full-time workers" over50 "Over 50 y.o." primeage "25-49" under25 "Under 25 y.o." marginal "Marginally employed" nonmarginal "Non-marginally employed") postfoot("\tabnotes{5}{This table displays the average marginal effect of the linear time trend in a linear regression, where the dependent variable is the AMEs of the year dummies from the main regression specification (Poisson for the UK, logistic for the GSS and SOEP, linear regression for the HRS) with the sample restricted to the subgroup of interest. Standard errors in parentheses. *\textit{p} < 0.05, **\textit{p} < 0.01, ***\textit{p} < 0.001}") width(\hsize) gaps booktab nonumbers keep(temp perm parttime fulltime newstarters nonnewstarters male female degree nodegree over50 primeage under25 marginal nonmarginal)

////////////////////////////////////////////////////////////////////////////////
*TABLE F.1: P-values from F-test of equality of time trends across disjoint groups
////////////////////////////////////////////////////////////////////////////////

esttab gss_t hrs_t bhps_t soep_t using "joint_p.tex", replace cells(joint_p(star pvalue(joint_p) fmt(%9.2f))) stats(margins_N, labels(N) fmt(0)) order(temp parttime newstarters male over50 over25 degree marginal) title("P-values for test of equal time trends by subgroup") collabels("") mlabels("US (GSS)" "US (HRS)" "UK" "Germany") coeflabels(temp "Temporary vs. permanent workers" parttime "Parttime vs. fulltime workers" newstarters "New starters vs. incumbent workers" male "Male vs. female" over50 "Over-50s vs. 25-49 vs. under-25s" degree "Tertiary degree holders vs. no degree" marginal "Marginally- vs. non-marginally employed") width(\hsize) gaps booktab nonumbers keep(temp parttime newstarters male degree over50 marginal) postfoot("\tabnotes{5}{This table displays the p-value for an F test that the time trends for the two subgroups in question are equal. *\textit{p} < 0.05, **\textit{p} < 0.01, ***\textit{p} < 0.001}")

////////////////////////////////////////////////////////////////////////////////

*Figures F.1-F.7: Year dummy mfx by subgroup, combined across countries

////////////////////////////////////////////////////////////////////////////////

cd "$main"

*Temporary vs. permanent workers
use "$ukdata/temp/bhps_temp.dta", clear

twoway (scatter dydx_temp year_temp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_perm year_perm, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_temp ci_low_temp year_temp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_perm ci_low_perm year_perm, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Temporary workers") label(2 "Permanent workers") order(1 2)) title("UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g1, replace)

use "$germanydata/temp/soep_temp.dta", clear

twoway (scatter dydx_temp year_temp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_perm year_perm, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_temp ci_lower_temp year_temp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_perm ci_lower_perm year_perm, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1985(2)2018, angle(45)) scale(.8) legend(label(1 "Temporary workers") label(2 "Permanent workers") order(1 2)) title("Germany 1985-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g2, replace)

graph combine g1.gph g2.gph, cols(2) graphregion(color(white)) ysize(2) xsize(5.5)
graph export "$othergraphics/temp_mfx.png", as(png) replace

*Part-time vs. full-time workers
use "$usdata/temp/gss_parttime.dta", clear

twoway (scatter dydx_parttime year_parttime, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_fulltime year_fulltime, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_parttime ci_lower_parttime year_parttime, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_fulltime ci_lower_fulltime year_fulltime, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Part-time") label(2 "Full-time") order(1 2)) title("US 1978-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g1, replace)

use "$usdata/temp/hrs_parttime.dta", clear

twoway (scatter dydx_parttime year_parttime, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_fulltime year_fulltime, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_parttime ci_lower_parttime year_parttime, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_fulltime ci_lower_fulltime year_fulltime, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1994(2)2016, angle(45)) scale(.8) legend(label(1 "Part-time") label(2 "Full-time") order(1 2)) title("US 1994-2016, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g2, replace)

use "$ukdata/temp/bhps_parttime.dta", clear

twoway (scatter dydx_parttime year_parttime, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_fulltime year_fulltime, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_parttime ci_low_parttime year_parttime, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_fulltime ci_low_fulltime year_fulltime, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Part-time") label(2 "Full-time") order(1 2)) title("UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g3, replace)

use "$germanydata/temp/soep_parttime.dta", clear

twoway (scatter dydx_parttime year_parttime, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_fulltime year_fulltime, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_parttime ci_lower_parttime year_parttime, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_fulltime ci_lower_fulltime year_fulltime, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1985(2)2018, angle(45)) scale(.8) legend(label(1 "Part-time") label(2 "Full-time") order(1 2)) title("Germany 1985-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g4, replace)

graph combine g1.gph g2.gph g3.gph g4.gph, rows(2) cols(2) graphregion(color(white)) ysize(2) xsize(2.75)
graph export "$othergraphics/parttime_mfx.png", as(png) replace

*Male vs. female workers
use "$usdata/temp/gss_sex.dta", clear

twoway (scatter dydx_male year_male, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_female year_female, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_male ci_lower_male year_male, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_female ci_lower_female year_female, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Male") label(2 "Female") order(1 2)) title("US 1978-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g1, replace)

use "$usdata/temp/hrs_sex.dta", clear

twoway (scatter dydx_male year_male, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_female year_female, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_male ci_lower_male year_male, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_female ci_lower_female year_female, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1994(2)2016, angle(45)) scale(.8) legend(label(1 "Male") label(2 "Female") order(1 2)) title("US 1994-2016, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g2, replace)

use "$ukdata/temp/bhps_sex.dta", clear

twoway (scatter dydx_male year_male, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_female year_female, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_male ci_low_male year_male, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_female ci_low_female year_female, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Male") label(2 "Female") order(1 2)) title("UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g3, replace)

use "$germanydata/temp/soep_sex.dta", clear

twoway (scatter dydx_male year_male, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_female year_female, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_male ci_lower_male year_male, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_female ci_lower_female year_female, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1985(2)2018, angle(45)) scale(.8) legend(label(1 "Male") label(2 "Female") order(1 2)) title("Germany 1985-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g4, replace)

graph combine g1.gph g2.gph g3.gph g4.gph, rows(2) cols(2) graphregion(color(white)) ysize(2) xsize(2.75)
graph export "$othergraphics/sex_mfx.png", as(png) replace

*Self-employed vs. non-self-employed

use "$usdata/temp/gss_semp.dta", clear

twoway (scatter dydx_semp year_semp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_nonsemp year_nonsemp, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_semp ci_lower_semp year_semp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_nonsemp ci_lower_nonsemp year_nonsemp, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Self-employed") label(2 "Not self-employed") order(1 2)) yline(0, lcolor(black*2)) title("US 1978-2018, adjusted for composition") xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g1, replace)

use "$ukdata/temp/bhps_semp.dta", clear

twoway (scatter dydx_semp year_semp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_nonsemp year_nonsemp, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_semp ci_low_semp year_semp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_nonsemp ci_low_nonsemp year_nonsemp, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Self-employed") label(2 "Not self-employed") order(1 2)) yline(0, lcolor(black*2)) title("UK 1991-2018, adjusted for composition") xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g2, replace)

use "$germanydata/temp/soep_semp.dta", clear

twoway (scatter dydx_semp year_semp, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_nonsemp year_nonsemp, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_semp ci_lower_semp year_semp, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_nonsemp ci_lower_nonsemp year_nonsemp, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1985(2)2018, angle(45)) scale(.8) legend(label(1 "Self-employed") label(2 "Not self-employed") order(1 2)) yline(0, lcolor(black*2)) title("Germany 1985-2018, adjusted for composition") xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g3, replace)

graph combine g1.gph g2.gph g3.gph, rows(2) cols(2) graphregion(color(white)) ysize(2) xsize(2.75)
graph export "$othergraphics/semp_mfx.png", as(png) replace

*University educated vs. non-university educated workers

use "$usdata/temp/gss_educ.dta", clear

twoway (scatter dydx_educated year_educated, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_uneducated year_uneducated, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_educated ci_lower_educated year_educated, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_uneducated ci_lower_uneducated year_uneducated, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "University degree") label(2 "No degree") order(1 2)) title("US 1978-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g1, replace)

use "$usdata/temp/hrs_educ.dta", clear

twoway (scatter dydx_educated year_educated, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_uneducated year_uneducated, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_educated ci_lower_educated year_educated, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_uneducated ci_lower_uneducated year_uneducated, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1994(2)2016, angle(45)) scale(.8) legend(label(1 "University degree") label(2 "No degree") order(1 2)) title("US 1994-2016, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g2, replace)

use "$ukdata/temp/bhps_educ.dta", clear

twoway (scatter dydx_educated year_educated, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_uneducated year_uneducated, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_educated ci_low_educated year_educated, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_uneducated ci_low_uneducated year_uneducated, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "University or professional degree") label(2 "No university degree") order(1 2)) title("UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g3, replace)

use "$germanydata/temp/soep_educ.dta", clear

twoway (scatter dydx_educated year_educated, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_uneducated year_uneducated, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_educated ci_lower_educated year_educated, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_uneducated ci_lower_uneducated year_uneducated, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1985(2)2018, angle(45)) scale(.8) legend(label(1 "Tertiary degree") label(2 "No degree") order(1 2)) title("Germany 1985-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g4, replace)

graph combine g1.gph g2.gph g3.gph g4.gph, rows(2) cols(2) graphregion(color(white)) ysize(2) xsize(2.75)
graph export "$othergraphics/educ_mfx.png", as(png) replace

*New starters vs. non-new starters

use "$ukdata/temp/bhps_jobtenure.dta", clear

twoway (scatter dydx_new year_new, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_old year_old, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_hi_new ci_low_new year_new, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_old ci_low_old year_old, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "New starters") label(2 "Non-new starters") order(1 2)) title("UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g1, replace)

use "$germanydata/temp/soep_jobtenure.dta", clear

twoway (scatter dydx_new year_new, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_old year_old, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_new ci_lower_new year_new, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_old ci_lower_old year_old, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1985(2)2018, angle(45)) scale(.8) legend(label(1 "New starters") label(2 "Non-new starters") order(1 2)) title("Germany 1985-2018, adjusted for composition") yline(0, lcolor(black*2))xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g2, replace)

graph combine g1.gph g2.gph, cols(2) graphregion(color(white)) ysize(2) xsize(5.5)
graph export "$othergraphics/jobtenure_mfx.png", as(png) replace

*Over-50s vs. prime age (25-49) vs. under-25s

use "$usdata/temp/gss_age.dta", clear

twoway (scatter dydx_over year_over, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_primeage year_primeage, connect(direct) msize(small) lcolor(gs7) mcolor(gs7)) (scatter dydx_under year_under, connect(direct) msize(small) lcolor(gs12) mcolor(gs12)) (rcap ci_upper_over ci_lower_over year_over, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_primeage ci_lower_primeage year_primeage, msize(0) lcolor(gs7) mcolor(gs7)) (rcap ci_upper_under ci_lower_under year_under, msize(0) lcolor(gs12) mcolor(gs12)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Over 50") label(2 "25-49") label(3 "Under 25") order(1 2 3)) title("US 1978-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g1, replace)

use "$ukdata/temp/bhps_age.dta", clear

twoway (scatter dydx_over year_over, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_primeage year_primeage, connect(direct) msize(small) lcolor(gs7) mcolor(gs7)) (scatter dydx_under year_under, connect(direct) msize(small) lcolor(gs12) mcolor(gs12)) (rcap ci_hi_over ci_low_over year_over, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_hi_primeage ci_low_primeage year_primeage, msize(0) lcolor(gs7) mcolor(gs7)) (rcap ci_hi_under ci_low_under year_under, msize(0) lcolor(gs12) mcolor(gs12)), graphregion(color(white)) bgcolor(white) xlabel(1991(2)2018, angle(45)) scale(.8) legend(label(1 "Over 50") label(2 "25-49") label(3 "Under 25") order(1 2 3)) title("UK 1991-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g2, replace)

use "$germanydata/temp/soep_age.dta", clear

twoway (scatter dydx_over year_over, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_primeage year_primeage, connect(direct) msize(small) lcolor(gs7) mcolor(gs7)) (scatter dydx_under year_under, connect(direct) msize(small) lcolor(gs12) mcolor(gs12)) (rcap ci_upper_over ci_lower_over year_over, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_primeage ci_lower_primeage year_primeage, msize(0) lcolor(gs7) mcolor(gs7)) (rcap ci_upper_under ci_lower_under year_under, msize(0) lcolor(gs12) mcolor(gs12)), graphregion(color(white)) bgcolor(white) xlabel(1985(2)2018, angle(45)) scale(.8) legend(label(1 "Over 50") label(2 "25-49") label(3 "Under 25") order(1 2 3)) title("Germany 1985-2018, adjusted for composition") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect") saving(g3, replace)


graph combine g1.gph g2.gph g3.gph, cols(2) graphregion(color(white)) ysize(2) xsize(2.75) 
graph export "$othergraphics/age_mfx.png", as(png) replace

cd "$ukdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

cd "$usdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

cd "$germanydata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}
